【问题标题】:I want to do pagination, but its not working Method Illuminate\Database\Eloquent\Collection::links does not exist我想做分页,但它不工作 Method Illuminate\Database\Eloquent\Collection::links 不存在
【发布时间】:2023-03-24 17:10:01
【问题描述】:

控制器: 这是整个控制器代码

  <?php

    namespace App\Http\Controllers;

    use App\Exam_sched;
    use App\Subject;
    use App\Batch;
    use Session;
    use Illuminate\Http\Request;

    class ExamSchedController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        class ExamSchedController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $exam_scheds= Exam_sched::paginate(3);
        return view('examschedule',compact('exam_scheds'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $exam_scheds=Exam_sched::all();
        $subjects=Subject::all();
        $batches=Batch::all();
        return view('examschedule', compact('exam_scheds','subjects','batches'));
    }


        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            $request->validate([
                "date"=>"required",
                "subject_id"=>"required",
                "batch_id"=>"required",
                "roomNo"=>"required",
                "startTime"=>"required",
                "endTime"=>"required"

            ]);

            // $rules= array(
            //     "date"=>"required",
            //     "subject_id"=>"required",
            //     "batch_id"=>"required",
            //     "roomNo"=>"required",
            //     "startTime"=>"required",
            //     "endTime"=>"required"
            // );
            // $this->validate($request, $rules);

            $exam_sched= new Exam_sched;

            $exam_sched->date=$request->date;
            $exam_sched->subject_id=$request->subject_id;
            $exam_sched->batch_id=$request->batch_id;
            $exam_sched->roomNo=$request->roomNo;
            $exam_sched->startTime=$request->startTime;
            $exam_sched->endTime=$request->endTime;

            $exam_sched->save();

            Session::flash("message","New Schedule has been added");

            return redirect('/examschedule');
        }

        /**
         * Display the specified resource.
         *
         * @param  \App\Exam_sched  $exam_sched
         * @return \Illuminate\Http\Response
         */
        public function show(Exam_sched $exam_sched)
        {
            //
        }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  \App\Exam_sched  $exam_sched
         * @return \Illuminate\Http\Response
         */
        public function edit($id)
     // public function edit()
        {

            $exam_sched = Exam_sched::find($id);
            $subjects=Subject::all();
            $batches=Batch::all();

            return view('editschedule',compact('exam_sched','subjects','batches'));
            // return view('examschedule');
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \App\Exam_sched  $exam_sched
         * @return \Illuminate\Http\Response
         */
         // public function update(Request $request, Exam_sched $exam_sched)
        public function update(Request $request, $id)
        {
            $exam_sched= Exam_sched::find($id);

            $rules= array(
                "date"=>"required",
                "subject_id"=>"required",
                "batch_id"=>"required",
                "roomNo"=>"required",
                "startTime"=>"required",
                "endTime"=>"required"
            );

            $this->validate($request, $rules);

            $exam_sched= Exam_sched::find($id);
            $exam_sched->date=$request->date;
            $exam_sched->batch_id=$request->batch_id;
            $exam_sched->subject_id=$request->subject_id;
            $exam_sched->roomNo=$request->roomNo;
            $exam_sched->startTime=$request->startTime;
            $exam_sched->endTime=$request->endTime;

            $exam_sched->save();

            Session::flash("message","Schedule has been updated!");

            return redirect('/examschedule');
        }


        /**
         * Remove the specified resource from storage.
         *
         * @param  \App\Exam_sched  $exam_sched
         * @return \Illuminate\Http\Response
         */
        public function delete($id)

        {   $exam_sched= Exam_sched::find($id);
            $schedToRemove=Exam_sched::find($id);
            $schedToRemove->delete();
            // Session::flash("message","Successfully Deleted!");
            return redirect('/examschedule')->with('success','Data Deleted');
        }
    }



Route:



    Route::get('/examschedule', 'ExamSchedController@index');

View Blade:


                <tbody>
                    @if($exam_scheds->count())
                    @foreach($exam_scheds as $exam_sched)
                    <tr class="tbody">
                        <td>{{$exam_sched->date}}</td>
                        <td>{{$exam_sched->batch->name}}</td>
                        <td>{{$exam_sched->subject->name}}</td>
                        <td>{{$exam_sched->roomNo}}</td>                        
                        <td>{{$exam_sched->startTime}}</td>
                        <td>{{$exam_sched->endTime}}</td>   
                        <td>{{$exam_sched->created_at->diffForHumans()}}</td>                   

                    </tr>
                    @endforeach
                    @endif
                </tbody>
            </table>
                {{ $exam_scheds->links() }}

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

请帮助我,我是 laravel 的新手 提前致谢

【问题讨论】:

  • 我在这里没有看到任何错误。您是否包含了整个控制器方法代码。您是否对视图中的 $exam_scheds 变量做了更多操作?
  • 我发布了整个控制器代码

标签: laravel laravel-blade laravel-6 laravel-pagination


【解决方案1】:

好的,现在我看到了问题。

在您使用的控制器的index 方法中

$exam_scheds= Exam_sched::paginate(3);

但是在create 中你使用的方法:

 $exam_scheds=Exam_sched::all();

并且您在这两种方法中使用相同的视图。

当然当你使用all()方法时没有分页所以你不能使用links then in view。

因此,您可能应该将create 方法更改为也与index 方法中相同的分页。

【讨论】:

  • 我还有一个问题,我想对数据进行排序,但它也不起作用
  • @cute 你可以做Exam_sched::orderBy('some_column_here')-&gt;paginate(3); 如果没有帮助请提出新问题
  • 好吧,我给你看上面的代码,我用的是 kyslik/column 可排序包
  • 我在上面编辑了我的代码,你能检查一下吗..谢谢
  • @cute 我不知道这个包。正如我所写的那样,您应该为此提出新问题,然后也许有人可以帮助您。
猜你喜欢
  • 2022-11-11
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2020-11-21
  • 2021-09-08
  • 2020-11-13
相关资源
最近更新 更多