Jac*_*son 2 php laravel eloquent
我有两个型号Job和User。
用户可以被分配多个作业,作业也可以有多个受让人。
class Job extends Model {
public function assignees()
{
return $this->belongsToMany('App\Models\User')->withTimestamps();
}
}
class User extends Model {
public function jobs()
{
return $this->belongsToMany('App\Models\Job')->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的作业控制器中,我正在更新受让人,如下所示:
$job = Job::find(1);
$job->assignees()->sync([1,2]);
$job->save();
Run Code Online (Sandbox Code Playgroud)
除了作业的时间戳没有更新之外,一切都按预期进行。
该updated_at字段保持不变。
谁能看到我的问题可能出在哪里?
参考文档,这应该可以完成工作: https://laravel.com/docs/7.x/eloquent-relationships#touching-parent-timestamps
您的模型应该包含一个新的数组属性$touches,该属性获取带有关系名称的新项目。
class Job extends Model {
//if you also want to update a user model from the jobs site
protected $touches = ['assignees'];
public function assignees()
{
return $this->belongsToMany('App\Models\User')->withTimestamps();
}
}
class User extends Model {
protected $touches = ['jobs'];
public function jobs()
{
return $this->belongsToMany('App\Models\Job')->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句: -方法withTimestamps()仅更新中间表的时间戳。
| 归档时间: |
|
| 查看次数: |
1417 次 |
| 最近记录: |