在Laravel 5中更新其他数据透视表值
I'm building a friendship system in Laravel 5 that requires a user to accept a potential friendship with another user.
I've created a many-to-many relationship between users and "helpers" (other users). The relationships look like this:
// Accepted Friendships
function friendsOfMine()
{
return $this->belongsToMany('App\User', 'helpers', 'user_id', 'helper_id')
->wherePivot('accepted', '=', 1);
}
// Potential Friendships
function potentialFriendsOfMine()
{
return $this->belongsToMany('App\User', 'helpers', 'user_id', 'helper_id')
->wherePivot('accepted', '=', 0);
}
The only difference between them is an additional value in my "helpers" pivot table: "accepted." "Accepted" is a boolean that defaults to 0.
I create the table row when a user first proposes friendship, and then would like to update the row when the friendship is accepted. Here is a version of my controller that condenses the two actions into a single code block:
public function store()
{
$currentUser = Auth::user();
$input = Input::get('helper_id');
$helper = User::find($input);
/* Creates "unaccepted" relationship */
$currentUser->friendsOfMine()->attach($helper);
/* Fetches newly created relationship */
$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input);
/* Updates "accepted" column and saves */
$accepted->pivot->accepted = 1;
$accepted->pivot->save();
Flash::success('You are now following this user.');
return Redirect::back();
}
However, I'm getting an error on this line—$accepted->pivot->accepted = 1;
—that reads: Creating default object from empty value.
In this case, how would I access and update a value in a pivot table record that I just created?
我正在Laravel 5中建立友谊系统,要求用户接受与其他用户的潜在友谊。
我在用户和“帮助者”(其他用户)之间创建了多对多关系。 关系看起来像这样: p>
//接受的友谊
underAdofOfMine()
{
返回$ this-> belongsToMany('App \ User','helpers ','user_id','helper_id')
- > wherePivot('accepted','=',1);
}
//潜在的友谊
function potentialFriendsOfMine()
{
返回 $ this-> belongsToMany('App \ User','helpers','user_id','helper_id')
- > wherePivot('accepted','=',0);
}
代码> pre>
它们之间的唯一区别是我的“帮助者”数据透视表中的一个附加值:“已接受”。 “Accepted”是一个默认为0的布尔值。 p>
我在用户首次提出友谊时创建表行,然后在接受友谊时更新行。 这是我的控制器的一个版本,它将两个动作压缩成一个代码块: p>
public function store()
{
$ currentUser = Auth :: user( );
$ input = Input :: get('helper_id');
$ helper = User :: find($ input);
/ *创建“unaccepted”关系* /
$ currentUser-&gt ; friendsOfMine() - > attach($ helper);
/ *获取新创建的关系* /
$ accepted = $ currentUser-> potentialFriendsOfMine() - > where('helper_id',$ input) ;
/ *更新“已接受”列并保存* /
$ accepted-> pivot-> accepted = 1;
$ accepted-> pivot-> save();
Flash: :success('你现在正在关注这个用户。');
返回Redirect :: back();
}
code> pre>
但是,我得到了 此行上的错误 - $ accepted-> pivot-> accepted = 1; code> - 读取:从空值创建默认对象。 p>
在此 如何,我如何访问和更新我刚创建的数据透视表记录中的值? p>
DIV>
This line:
$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input);
returns a Builder object. I think you have forgotten to add ->get()
at the end to get a model
object.
In other words:
$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input)->get();
OR
$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input)->first();