我有以下型号
ReleasePackages
<?php
namespace 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
<?php
namespace 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
]
我怎样才能更改代码以使关系正常工作?
小怪兽爱吃肉