Laravel 4 拉威尔4号雄辩
我有没有办法用雄辩的口才做到这一点Laravel 4 拉威尔4号雄辩,laravel-4,eloquent,Laravel 4,Eloquent,我有没有办法用雄辩的口才做到这一点 $orders = Customer::with('orders','orders.shop')->where('orders.shop.location','=','Japan')->get() 客户、订单和店铺是一个表,其中一个客户有多个订单,而每个订单只有一个店铺 位置是shop表中的一列 我不断收到一个错误,说明orders.shop.location是一个未找到的列 有人能帮忙吗?提前感谢。您需要在模型类中定义关系 客户模式: publ
$orders = Customer::with('orders','orders.shop')->where('orders.shop.location','=','Japan')->get()
客户、订单和店铺是一个表,其中一个客户有多个订单,而每个订单只有一个店铺
位置是shop表中的一列
我不断收到一个错误,说明orders.shop.location是一个未找到的列
有人能帮忙吗?提前感谢。您需要在模型类中定义关系 客户模式:
public function orders()
{
return $this->hasMany('Order');
}
订单型号:
public function customer()
{
return $this->belongsTo('Customer');
}
然后,如果您需要特殊客户的订单,您只需执行以下操作:
$orders = Customer::find($id)->orders;
或查找附加到订单的用户:
$user = Order::find($id)->user;
您还可以在您的店铺和订单模型之间使用相同的关系,并执行以下操作:
$orders = Order::with(array('shop' => function($query)
{
$query->where('location', '=', 'japan');
}))->get();
这将为您提供位于日本的商店的所有订单
有关此类型请求的更多信息:
在CustomerModel中,您需要设置一个关系(一对多): 在OrderModel中:
public function costumer()
{
return $this->belongsTo('CostumerModel', 'foreign_key_in_orderTable');
}
然后,在OrderModel中,与商店建立一个或多个关系(一对一):
现在在ShopModel中(一对一):
查询:
$orders = Customer::with('costumer', 'shop')->where('location','=','Japan')->get();
您是否在模型中设置了关系?
public function shop()
{
return $this->hasOne('ShopModel', 'foreign_key');
}
public function order()
{
return $this->belongsTo('OrderModel', 'local_key');
}
$orders = Customer::with('costumer', 'shop')->where('location','=','Japan')->get();