ManyToMany关系 - 如何在数据透视表中更新属性 [英] ManyToMany relation - how update attribute in pivot table
问题描述
我正在学习使用数据透视表: https://我有$ code> WeeklyRoutine 模型。每个例程都有几个活动。分配的活动附加在数据透视表 activity_routine
中。
WeeklyRoutine中定义的关系
model:
return $ this-> belongsToMany('App \Models\Activity' ,'activity_routine','routine_id','activity_id') - > withPivot('done_at') - > withTimestamps();
}
它看起来像这样:
// activity_routine pivot table(仅相关列)
| id | activity_id | routine_id | done_at |
| 34 | 1 | 4 | 2016-04-23 09:27:27 | // * 1
| 35 | 2 | 4 | null | // * 2
* 1此活动标记为完成以下代码
* 2这个活动尚未完成
我有什么:
我可以更新 done_at
字段,从而使其标记为给定周的DONE(上述代码中的weeklyroutine_id = 4)
public function make_an_activity_complete($ routineid,$ activityid){
$ date = new \DateTime;
$ object = Routine :: find($ routineid) - > activities() - > updateExistingPivot($ activityid,array('done_at'=> $ date));
return'done $'
我需要什么
我想要一个活动,当它已经完成,那就是 done_at
不是 null
buc包含一个日期,使它 null
。
换句话说,我需要做以下切换价值,但适当的方式: p>
$ pivot = DB :: table('activity_routine') - > where('routine_id,$ routineid) - > where( 'activity_id,$ activityid) - > first();
if($ pivot-> done_at!= null){
$ new_val = new \DateTime;
} else {
$ new_val = null;
}
$ object = Routine :: find($ routineid) - > activities() - > updateExistingPivot($ activityid,array('done_at'=> $ new_val));
如何做?我没有线索!
Thx。
您的方法对我来说似乎不错。我可能会这样做。
$ routine = Routine :: find($ routineid);
$ activity = $ routine-> activities() - > find($ activityid);
$ done_at = is_null($ activity-> pivot-> done_at)? new \DateTime:null;
$ routine-> activities() - > updateExistingPivot($ activityid,compact('done_at'));
I am now learning to work with pivot tables: https://laravel.com/docs/4.2/eloquent#working-with-pivot-tables
I have WeeklyRoutine
model. Each routine has several Activities. The assigned activities are attached in a pivot table activity_routine
.
Relation defined in the WeeklyRoutine
model:
return $this->belongsToMany('App\Models\Activity', 'activity_routine', 'routine_id', 'activity_id')->withPivot('done_at')->withTimestamps();
}
it looks like this:
// activity_routine pivot table (relevant columns only)
| id | activity_id | routine_id | done_at |
| 34 | 1 | 4 | 2016-04-23 09:27:27 | // *1
| 35 | 2 | 4 | null | // *2
*1 this activity is marked as done with the code below *2 this activity is not yet done
what I have:
I can update the done_at
field in the pivot table, thus making it marked as DONE for the given week (a weeklyroutine_id = 4 in the above code
public function make_an_activity_complete($routineid, $activityid) {
$date = new \DateTime;
$object = Routine::find($routineid)->activities()->updateExistingPivot($activityid, array('done_at' => $date));
return 'done!';
}
what I need
I want to UN-DO an activity. When it is already done, that is when the done_at
is not null
buc contains a date, make it null
.
In other words I need to do the below switch of value, but the proper way:
$pivot = DB::table('activity_routine')->where('routine_id, $routineid)->where('activity_id, $activityid)->first();
if($pivot->done_at != null) {
$new_val = new \DateTime;
} else {
$new_val = null;
}
$object = Routine::find($routineid)->activities()->updateExistingPivot($activityid, array('done_at' => $new_val));
How to do it? I have no clue!
Thx.
Your approach seems fine to me. I would probably do it like this.
$routine = Routine::find($routineid);
$activity = $routine->activities()->find($activityid);
$done_at = is_null($activity->pivot->done_at) ? new \DateTime : null;
$routine->activities()->updateExistingPivot($activityid, compact('done_at'));
这篇关于ManyToMany关系 - 如何在数据透视表中更新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!