如何使用Laravel获取表中的下一条记录?

问题描述 投票:0回答:2

我的数据库中有一个名为job_seeker_profiles的表,我在其中存储寻求工作者的资料。我有一个名为AdminEmployerSearchController的控制器,使用此show方法,雇主可以在其中查看求职者的个人资料:

public function show()
    {
        $jobSeekerProfiles = JobSeekerProfile::limit(1)->get();

        return view('admin.employer.search.show', compact('jobSeekerProfiles'));
    }

正在向我显示表中的第一条记录,这是我想要的,一次显示一条记录。我的视图中有一个名为“跳过”的按钮,单击该按钮时,我希望它显示数据库中的下一条记录,现在它向我显示记录1,因此,单击该按钮时,它将显示到记录2,然后是记录3,然后记录4等。就像上一个和下一个功能。

show.blade.php文件:

@extends('layouts.admin')

@section('content')

    @if($jobSeekerProfiles)

        @foreach($jobSeekerProfiles as $jobSeekerProfile)

            <div class="row">
                <div class="col-md-12">
                    <video width="100%" height="100%" controls>
                        <source src="{{ asset('videos/1583095973A man DJing an event.mp4') }}" type="video/mp4">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="col-md-6">
                    <br>
                    <h1 class="h1-admin">{{$jobSeekerProfile->first_name}} {{$jobSeekerProfile->last_name}}</h1>
                </div>
                <div class="col-md-6 text-right">
                    <br>
                    <video width="100" height="auto" controls>
                        <source src="{{ asset('videos/1583095973Man Doing Rap Music.mp4') }}" type="video/mp4">
                        Your browser does not support the video tag.
                    </video>
                    <video width="100" height="auto" controls>
                        <source src="{{ asset('videos/1583095973Tractor Cutting Grass In The Field.mp4') }}" type="video/mp4">
                        Your browser does not support the video tag.
                    </video>
                </div>
                <div class="col-md-12">
                    <h3>Experience:</h3>
                    <p>{{$jobSeekerProfile->experience}}</p>
                    <h3>Additional Skills:</h3>
                    <p>{{$jobSeekerProfile->additional_skills}}</p>
                </div>
                <div class="col-md-6">
                    <button type="button" class="btn btn-lg btn-block btn-danger">Skip</button>
                </div>
                <div class="col-md-6">
                    <button type="button" class="btn btn-lg btn-block btn-success">Interview</button>
                </div>
                <div class="col-md-12">
                    <br><br>
                    <hr><br><br>
                </div>
            </div>

        @endforeach

    @endif

@stop

我以为我必须先在表中找到ID,然后再使用它,但是我似乎无法使其正常工作。以下是该页面的屏幕截图,您将明白我的意思。enter image description here

php mysql laravel eloquent
2个回答
0
投票

您应该使用->skip()->offset()方法定义起点,并使用->take()方法获取数据数量

JobSeekerProfile::get()->offset(2)->take(1);

将返回第三个结果,跳过前两个记录。


0
投票

您需要使用分页来做到这一点:

$profiles = JobSeekerProfile::paginate(1);
return view('index', ['profiles' => $profiles]);

https://laravel.com/docs/5.8/pagination

© www.soinside.com 2019 - 2024. All rights reserved.