laravel 方法 Illuminate\Database\Eloquent\Collection::links 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56278120/
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 18:20:20  来源:igfitidea点击:

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

laravel

提问by smouk

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.

我在 User 和 Message 之间创建了模型关系。我想为经过身份验证的用户实现消息列表,但出现以下错误。

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

方法 Illuminate\Database\Eloquent\Collection::links 不存在

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

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

“方法Illuminate\Database\Eloquent\Collection::links 不存在。(查看:C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)”

采纳答案by Eduardo Hernandez

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:

检查您的视图刀片,该方法 links() 只能在您的数据模型实现分页方法时使用。如果你不使用 paginate 删除这部分:

 {{$messages->links()}}

回答by stanley mbote

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

使用控制器中的分页方法,您可以调用 links 方法在视图中对对象进行分页,如下所示

 {{$user->links()}}

hope it helps you

希望对你有帮助

回答by Mr. X. In Bombay.

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);
    }