belongsTo方法返回null

时间:2015-06-07 15:24:55

标签: php laravel laravel-4 eloquent

我有两个名为Customer的模型,第二个是Website

他们之间的关系是Customer hasMany WebsiteWebsite属于Customer

这就是我这样做的方式

class Website extends \Eloquent {
    use SubscriptionBillableTrait;

    protected $fillable = [];

    protected $guarded = ['id'];

    public function customermodel()
    {
        // Return an Eloquent relationship.
        return $this->belongsTo('Customer')
    }

}

Customer型号

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

1 个答案:

答案 0 :(得分:0)

使用此

$website = Website::find(1)->with('customermodel');
return dd($website->customermodel);