class Ingredient extends Eloquent {
public function unit() {
return $this->hasOne('IngredientUnit', 'id', 'unit_id');
}
}
class IngredientUnit extends Eloquent {
public function ingredient() {
return $this->belongsTo('Ingredient', 'unit_id', 'id');
}
public function getNamesAttribute() {
$quantity = $this->ingredient()->quantity; // <- ErrorException
...
}
}
ErrorException (E_UNKNOWN):Получить свойство связи в модели
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$quantity
Если удалить скобки - $this->ingredient->quantity;
- я
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Как я могу получить свойство объекта отношения (принадлежит Too)?
Schemas:
Schema::create('ingredients', function(Blueprint $table)
{
$table->increments('id');
$table->integer('recipe_id')->unsigned();
$table->integer('unit_id')->unsigned()->nullable();
$table->float('quantity')->unsigned();
...
});
Schema::create('ingredient_units', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
...
});
Отношение хорошее Ингредиент (id, quantity, unit_id, ...) | IngredientUnit (id, name, ...). Laravel docs: 'return $ this-> hasOne ('Phone', 'foreign_key', 'local_key');' so 'unit_id' является внешним и' id' является локальным – Shaddow
Я не понимаю вашу точку зрения. Если вы говорите, что 'Ingredient' имеет поле' unit_id', то и ваши отношения неверны. Таким образом, самый простой способ заключается в совместном использовании схемы таблиц. –
ok Я выложу схемы ... но все работает для меня, кроме этой модели, внутри использования отношения – Shaddow