Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Illuminate\Database\Eloquent\Collection::links does not exist

Tags:

laravel

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

like image 263
smouk Avatar asked May 23 '19 15:05

smouk


2 Answers

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() }}
like image 89
Eduardo Hernandez Avatar answered Nov 05 '22 10:11

Eduardo Hernandez


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

like image 7
stanley mbote Avatar answered Nov 05 '22 10:11

stanley mbote