调用未定义的方法(laravel 5.2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38868551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to undefined method (laravel 5.2)
提问by YaSh Chaudhary
I want to display friends of the user. But I am getting the following error:
我想显示用户的朋友。但我收到以下错误:
BadMethodCallException in Builder.php line 2345: Call to undefined method Illuminate\Database\Query\Builder::friends()
Builder.php 第 2345 行中的 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::friends()
FriendController:
朋友控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Auth;
class FriendController extends Controller
{
public function getIndex(){
$friends=Auth::user()->friends();
return view('friends',['friends'=>$friends]);
}
}
Route:
路线:
Route::get('/friends',[
'uses' => 'FriendController@getIndex',
'as' => 'friends',
'middleware' => 'auth:web'
]);
User model:
用户模型:
public function getName(){
if($this->firstname && $this->lastname){
return "{$this->firstname} {$this->lastname}";
}
if($this->firstname)
return $this->firstname;
return null;
}
public function getNameOrUsername(){
return $this->getName() ?: $this->username;
}
public function getFirstNameOrUsername() {
return $this->firstname ?: $this->username;
}
My view:
我的看法:
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Your Friends</h3>
@if(!$friends->count())
<p>you have no friends</p>
@else
@foreach($friends as $user)
@include('userblock')
@endforeach
@endif
</div>
userblock.blade:
userblock.blade:
<div class="media">
<a href="{{ route('myplace', ['username'=>$user->username]) }}" class="pull-left">
<img src="" class="media-object" alt="{{ $user->getNameOrUsername() }}">
</a>
<div class="media-body">
<h4 class="media-heading"><a href="#">{{ $user->getNameOrUsername() }}</a></h4>
</div>
采纳答案by jaysingkar
It seems like you are having many to many relationship between user
model as user and its friends(again a user).So, we can use belongsToMany()
method to retrieve the friends of selected user as Users
.
To do so, add the following function to your User
model.
看起来你在user
模型作为用户和它的朋友(又是一个用户)之间有很多对多的关系。所以,我们可以使用belongsToMany()
方法来检索所选用户的朋友作为Users
。
为此,请将以下函数添加到您的User
模型中。
public function friends(){
return $this->belongsToMany('App\User::class','friends','user_id','friend_id')->withPivot('accepted');
}
To get the friends of currently loged in User use following:
要获取当前登录用户的朋友,请使用以下命令:
$friends = Auth::user()->friends()->get();
回答by rap-2-h
Assuming friends
are related users and you have a friend_id
column in your user
table, you could to add a friends
method in your User
model:
假设friends
是相关用户并且您friend_id
的user
表中有一个列,您可以friends
在User
模型中添加一个方法:
public function friends()
{
return $this->hasMany(\App\User::class, 'friend_id');
}
You could read more about relationship here. You could also use a packagefor this need. Or search on SO about "self reference relation laravel"