属于返回类



当我在没有默认键的情况下使用belongsTo时,它不会连接。。

我的users表有user_id,它是表示该用户的一些rand和唯一字符串。我的url表有user_iduri列,其中user_id包含usersuser_id

在Url模型中,我有:

public function user() {
return $this->belongsTo('AppUser','user_id','user_id');
}

在用户模型中,我有:

public function uri() {
return $this->hasOne('AppUrl', 'user_id', 'user_id')->first()->uri;
}

利用CCD_ 7从与CCD_ 10相连的CCD_ 9表中得到CCD_。

但当我使用$url->user()时,我会在laravel修补程序中返回null或BelongsTo类。

有人知道为什么吗?

您的User::uri()方法不是关系方法。

您在其中使用了一个relationship方法,但您在查询第一个结果并返回uri属性。

您的Url::user()方法是一个关系方法,因为您实际上是在返回一个关系(BelongsTo(实例。显式关系的使用如下:

// To get a related entity on a BelongsTo relationship, you access it as a property:
$url->user
// To query a relationship, you use it as a method:
$url->user()->where(...)->first();

最新更新