我在Laravel 5中建立一个友谊系统,要求用户接受与另一个用户的潜在友谊。
我已经在用户和"帮助者"之间建立了多对多的关系。 (其他用户)。关系看起来像这样:
// 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);
}
他们之间的唯一区别是我的帮助""数据透视表:"接受。" "接受"是一个默认为0的布尔值。
我在用户首次提出友谊时创建表格行,然后在接受友谊时更新该行。这是我的控制器的一个版本,它将两个动作压缩成一个代码块:
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();
}
但是,我在这一行收到错误 - $accepted->pivot->accepted = 1;
- 其中包含:从空值创建默认对象。
在这种情况下,我如何访问和更新刚创建的数据透视表记录中的值?
答案 0 :(得分:3)
这一行:
->get()
返回一个Builder对象。我认为您忘记在最后添加model
以获取$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input)->get();
对象。
换句话说:
$accepted = $currentUser->potentialFriendsOfMine()->where('helper_id', $input)->first();
OR
Session.set('authCode',null);
Session.set('profile',null);
Template.home.events({
'click .viewLinkedinProfileButton' :function(event, template){
var redirect_uri = encodeURIComponent(Meteor.absoluteUrl('userAuthComplete'));
var client_id='XXXXXXX';
var state = 'myRandomString';
var scope = ['r_basicprofile'];
var url = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id="+client_id+"&redirect_uri="+redirect_uri+"&state="+state+"&scope=r_basicprofile";
showPopup(url,function(err){
if(err){
console.log("Cancelled "+err);
}
else{
Meteor.call('getAccessToken',Session.get('authCode'),function(err,res){
if(res){
console.log(res);
Session.set('profile',res);
}
});
Session.set('authCode',null);
}
})
}
});
function showPopup(url, callback, dimensions) {
// default dimensions that worked well for facebook and google
var popup = openCenteredPopup(
url,
(dimensions && dimensions.width) || 650,
(dimensions && dimensions.height) || 331
);
var checkPopupOpen = setInterval(function() {
try {
var popupClosed = popup.closed || popup.closed === undefined;
} catch (e) {
return;
}
if (popupClosed) {
var url = popup.document.URL;
if(url.toLowerCase().indexOf('code') !== -1){
var array1 = url.split('code=');
var array2 =array1[1].split('&');
Session.set('authCode',array2[0]);
clearInterval(checkPopupOpen);
callback();
}
else{
clearInterval(checkPopupOpen);
}
}
}, 50);
}
function openCenteredPopup(url, width, height) {
var screenX = typeof window.screenX !== 'undefined'
? window.screenX : window.screenLeft;
var screenY = typeof window.screenY !== 'undefined'
? window.screenY : window.screenTop;
var outerWidth = typeof window.outerWidth !== 'undefined'
? window.outerWidth : document.body.clientWidth;
var outerHeight = typeof window.outerHeight !== 'undefined'
? window.outerHeight : (document.body.clientHeight - 22);
var left = screenX + (outerWidth - width) / 2;
var top = screenY + (outerHeight - height) / 2;
var features = ('width=' + width + ',height=' + height +
',left=' + left + ',top=' + top + ',scrollbars=yes');
var newwindow = window.open(url, 'Login', features);
if (newwindow.focus)
newwindow.focus();
return newwindow;
}
Template.home.helpers({
profile:function(){
return Session.get('profile');
}
});