I created a model relationship between User and Message. I want to implement a list of messages for the authenticated user but I get the following error.
Method Illuminate\Database\Eloquent\Collection::links does not exist
Controller
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $user->message);
}
Message provider
class message extends Model
{
public function user() {
return $this->belongsTo('App\User');
}
}
User provider
public function message ()
{
return $this->hasMany('App\Message');
}
index.blade.php
@extends('layouts.app')
@section('content')
<h1>messages</h1>
@if(count($messages)>0)
@foreach ($messages as $message)
<div class="well">
<h3><a href="/message/{{$message->id}}">{{$message->user}}</a></h3>
<small>Written on {{$message->created_at}} </small>
</div>
@endforeach
{{$messages->links()}}
@else
<p> no post found </p>
@endif
@endsection
Error
"Method Illuminate\Database\Eloquent\Collection::links does not exist.(View: C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)"
links()
method in theindex.blade.php
file. Are you doing this? If not, are you callinglinks()
anywhere? – George Hansonlinks()
method in your view. You are specifically calling$messages->links()
. There is nolinks()
method on a Laravel Collection, are you trying to do pagination? – George Hansonlinks()
. Take a look:@endforeach {{$messages->links()}} @else
– mare96