我有以下型号ReleasePackages<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;class ReleasePackages extends Model{ public $timestamps = false; /** */ public function artifacts() { return $this->belongsToMany( 'App\Models\Artifacts', 'release_packages_artifacts', 'release_package_id', 'artifact_id' ); } public function created_by() { return $this->hasOne('App\Models\Users', 'id'); } public function approved_by() { return $this->hasOne('App\Models\Users', 'id'); }}和Users<?phpnamespace App\Models;use Illuminate\Database\Eloquent\Model;class Users extends Model{ public function release_packages() { return $this->hasOne('App\Models\ReleasePackages', 'created_by'); }}现在对于每个发布包,有两列与Users. 一个是created_by,另一个是approved_by。我试图通过上面的代码设置这种关系,但是当我调用$packages = ReleasePackages::with(['artifacts','created_by','approved_by'])->get();我没有与 . 建立关系Users。我在 users 表中有相应的用户 id,列名是 id。我从上面的控制器代码中得到以下响应: #relations: array:3 [▼ "artifacts" => Collection {#265 ▶} "created_by" => null "approved_by" => null ]我怎样才能更改代码以使关系正常工作?
1 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
你的关系错了created_by(),approved_by()应该是belongsTo
public function created_by()
{
return $this->belongsTo('App\Models\Users', 'created_by','id');
}
public function approved_by()
{
return $this->belongsTo('App\Models\Users', 'approved_by','id');
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消