У меня есть модель «Пользователь и заказ», в которой пользователь может иметь много заказов:
Код: Выделить всё
// Inside User model
public function orders()
{
return $this->hasMany('Order');
}
Код: Выделить всё
// Inside Order model
public function user()
{
return $this->belongsTo('User');
}
Код: Выделить всё
public function products()
{
return $this->belongsToMany('Product');
}
Однако, когда я выполняю следующее:
Код: Выделить всё
$users = User::with('orders')->find(1);
return $users;
Но когда я меняю порядок, он работает отлично:
Код: Выделить всё
$orders = Order::with('User')->get();
return $orders;
База данных:
id
user_id
notes
1
1
Это тестовый заказ
Подробнее здесь: https://stackoverflow.com/questions/267 ... using-with
Мобильная версия