Я думаю, что нет, такое невозможно. Нужна таблица в БД и модель.
Смотрим реализацию HasRelationships::belongsTo()
:
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string|null $foreignKey
* @param string|null $ownerKey
* @param string|null $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
$relation = $this->guessBelongsToRelation();
}
$instance = $this->newRelatedInstance($related);
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey)) {
$foreignKey = Str::snake($relation).'_'.$instance->getKeyName();
}
// Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations.
$ownerKey = $ownerKey ?: $instance->getKeyName();
return $this->newBelongsTo(
$instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
);
}
В конце вызывается
return $this->newBelongsTo(
$instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
);
А $instance->newQuery()
должно быть инстансом класса \Illuminate\Database\Eloquent\Builder
:
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
{
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
}
А метод newQuery()
находится в классе \Illuminate\Database\Eloquent\Model
, это и есть Модель. И он нужен
для построения запросов к БД.
Вывод: без модели не обойтись.
PS. Не знаю какую задачу вы хотите решить. Если из ряда "вытащить операции с названием 'оплачено'", то
можно использовать локальные скоупы.
Пример - это добавить в Модель Kassa:
/**
* @param Builder $query
* @return Builder
*/
public function scopePaid(Builder $query): Builder
{
return $query->where("operation_name_id", 1);
}
Пример использования:
Kassa::paid()->get();