调用未定义的方法(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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:17:20  来源:igfitidea点击:

Call to undefined method (laravel 5.2)

phplaravellaravel-5laravel-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 usermodel 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 Usermodel.

看起来你在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 friendsare related users and you have a friend_idcolumn in your usertable, you could to add a friendsmethod in your Usermodel:

假设friends是相关用户并且您friend_iduser表中有一个列,您可以friendsUser模型中添加一个方法:

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"

你可以在这里阅读更多关于关系的信息。你也可以使用一个来满足这个需求。或者搜索关于“自引用关系laravel”的SO