我有一個名爲Customer
第二個兩分型一個是Website
屬於關聯方法返回null
它們之間的關係是,Customer
的hasMany Website
而Website
屬於關聯Customer
這就是我正在做的這個
class Website extends \Eloquent {
use SubscriptionBillableTrait;
protected $fillable = [];
protected $guarded = ['id'];
public function customermodel()
{
// Return an Eloquent relationship.
return $this->belongsTo('Customer')
}
}
Customer
model
use Mmanos\Billing\CustomerBillableTrait;
class Customer extends \Eloquent {
use CustomerBillableTrait;
protected $fillable = [];
protected $guarded = ['id'];
public function websites() {
return $this->hasMany('Website');
}
}
當我試圖通過關係來訪問Customer
這樣
$website = Website::find(1);
return dd($website->customermodel);
,則返回null
注:我使用Laravel 4
不該」 t來自Eloquent關係的這些類名包含完全限定的名稱空間'$ this-> belongsTo ('App \ Customer');'(這很可能只適用於你使用Laravel 5,但你沒有指定)。 – Bogdan
聲明關係時使用全名空間,例如'$ this-> hasMany('App \ Models \ Website');'和'$ this-> belongsTo('App \ Models \ Customer');'。那樣有用嗎? – Ohgodwhy
@Bogdan我正在使用Laravel 4,所以我不認爲這會成爲觸發器 – Muhammad