[英]Call to undefined method Illuminate\Database\Eloquent\Collection::save() laravel
I made a function that brings me to the error: 我做了一个使我犯错误的函数:
Call to undefined method Illuminate\\Database\\Eloquent\\Collection::save()
I can't understand why it is throwing 我不明白为什么会扔
public function multiplelinkPlayerWithFamily($parent_id)
{
$player_id = Input::get('player_id');
$CheckRelationship = UsersRelationship::where('parent_user_id', $parent_id)->where('child_user_id', $player_id)->first();
if($CheckRelationship) {
return Response::json( [
'ok'=> false,
'message'=> 'The profiles are currently linked '
] ,422);
}
$user = User::find($player_id);
$user->parent_id = $parent_id;
$user->updated_by = $parent_id;
$user->save();
$UsersRelationship = new UsersRelationship;
$UsersRelationship->parent_user_id = $parent_id;
$UsersRelationship->child_user_id = $player_id;
$UsersRelationship->save();
$udata = ApiUserController::getLoginUserData();
return Response::json([
'ok'=> true,
'message'=> 'Linked',
'udata'=> $udata
] ,200);
}
I can't see how your models are structured, but it looks like you've actually turned a model into a relation: ie. 我看不到您的模型是如何构造的,但是看起来您实际上已经将模型转换为关系:即。 $UsersRelationship
It looks like you're trying to set up a many-to-many relation between users, correct? $UsersRelationship
似乎您正在尝试在用户之间建立多对多关系,对吗?
public function parents()
{
return $this->belongsToMany('User','users_relationships','child_id','parent_id');
}
public function children()
{
return $this->belongsToMany('User','users_relationships','parent_id','child_id');
}
Once you've got the relations set up right, you can call: 建立正确的关系后,您可以致电:
$user->parents()->attach($parent_id);
All multi-result sets returned by Eloquent, either via the get method or a relationship, will return a collection object. Eloquent通过get方法或关系返回的所有多结果集都将返回一个集合对象。 If the result of your query is likely to return more than one value, Laravel returns a collection. 如果查询的结果可能返回多个值,Laravel将返回一个集合。 Understanding query methods that returns a Collection would save you the problem of calling an Eloquent model method on a Collection. 了解返回集合的查询方法将为您节省在集合上调用Eloquent模型方法的问题。
Your query is most likely to return more than one record from the database or none from the database. 您的查询最有可能从数据库返回一个以上的记录,或者从数据库中返回一个以上的记录。 This would cause 'Eloquent' to return a Collection even if the number of records found is One. 即使找到的记录数为1,这也将导致'Eloquent'返回一个Collection。 Because the the returned value is a Collection, we would need to loop through the returned set to have access to the individual Model. 因为返回的值是Collection,所以我们需要遍历返回的集合以访问单个Model。
So try replace your below code: 因此,请尝试替换以下代码:
$user = User::find($player_id);
$user->parent_id = $parent_id;
$user->updated_by = $parent_id;
$user->save();
and replace it as like given below: 并按如下所示替换它:
$user = User::find($player_id);
$user->each(function($user)
{
$user->parent_id = $parent_id;
$user->updated_by = $parent_id;
$user->save();
});
Hope it may work. 希望它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.