Я хочу отобразить друзей пользователя. Но я получаю следующее сообщение об ошибке:Call to undefined method (laravel 5.2)
BadMethodCallException in Builder.php line 2345: Call to undefined method 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::get('/friends',[
'uses' => '[email protected]',
'as' => 'friends',
'middleware' => 'auth:web'
]);
Пользователь:
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;
}
Мое мнение:
<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:
<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>
Где описывается ваше «друзья» отношение отношение? –
@ rap-2-h u означает отношение между моделью друзей и моделью пользователя? –
Да :) У вас есть метод 'friends' в вашей модели User? –