I'm just getting started with Laravel so please forgive any noobness.
我有User
和Order
型号,用户有很多订单:
# Inside User model
public function orders()
{
$this->hasMany('Order');
}
# Inside Order
public function user()
{
return $this->belongsTo('User');
}
// Not sure if this is upsetting anything (also in Order)
public function products()
{
return $this->belongsToMany('Product');
}
所以我认为我有以上的权利.
But when I do this:
$users = User::with('orders')->find(1);
return $users;
我得Call to a member function addEagerConstraints() on null
分.
However, if I do it the other way around, it works great:
$orders = Order::with('User')->get();
return $orders;
What am I doing wrong / what don't I understand?! Or is my problem bigger than I think?
数据库: