6
votes

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)"

4
The error message looks like you are calling a links() method in the index.blade.php file. Are you doing this? If not, are you calling links() anywhere?George Hanson
@GeorgeHanson thanks for the answer but i dont think that i call links i m new with laravel i will share the index.blade viewsmouk
It looks like you are calling the links() method in your view. You are specifically calling $messages->links(). There is no links() method on a Laravel Collection, are you trying to do pagination?George Hanson
You are calling links(). Take a look: @endforeach {{$messages->links()}} @else mare96
@GeorgeHanson sorry i m new in stackoverflow and i have bad english excuse me but i m trying to show the messages of the user who is loged only his message like on the dashboardsmouk

4 Answers

11
votes

Check your view blade, that method (links()) only could be used when your data model is implementing paginate() method.

If you dont use paginate(), remove this part:

{{$messages->links() }}
6
votes

If you are trying to paginate your data when it gets to the view then you need to add the paginate in your controller before passing the data to the view. Example

return $users = Users::select('id','name')->paginate(10);

with that paginate method in your controller, you can call the links method to paginate your object in view as shown below

 {{$users->links()}}

hope it helps you

0
votes

You can do something like this in your controller file.

    public function index()
    {
        $messages = Message::all()->paginate(5);
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('message.index')->with('messages', $messages, $user->message);
    }
0
votes

There are 2 ways to resolve this issue:

  1. Either use paginate function while searching data from database:

$users = DB::table('users')->where('id',$user_id)->paginate(1);

  1. Remove links() function from index.blade.php

{{ $messages->links() }}