0
由於某種原因,我無法鏈接模型對象。我試圖爲'訂單'加載'位置',並希望將邏輯包含在模型本身中。但是過去的一個鏈條,它不起作用。Laravel模型對象鏈接
class Order extends Eloquent {
protected $table = 'orders';
public function customer() {
return $this->belongsTo('Customer');
public function location() {
return $this->customer()->location(); // this does not work
}
}
class Customer extends Eloquent {
protected $table = 'customers';
public function user() {
return $this->belongsTo('User');
}
public function orders() {
return $this->hasMany('Order');
}
public function location() {
return $this->user()->location();
// return $this->user(); // WORKS!!
}
}
class User extends Eloquent {
protected $table = 'users';
public function locations() {
return $this->hasMany('Location');
}
public function location() {
return $this->locations()->first();
}
}
我最終想要做到這一點:
class ChefController extends BaseController {
public function get_orders() {
$chef = $this->get_user_chef(); // this already works
return $chef->orders()->with('location')->get(); // does not work
}
}
這不是默認的嗎? –
那麼,當你命名你的方法,然後模型名稱,你必須明確的id。 – carousel
方法名稱與模型不同? –