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

时间:2020-03-08 17:45:35

标签: php mysql laravel eloquent

更新:下一个按钮仍然不起作用。 我必须更改一些代码,因为我的下一个按钮将从id 1变为2、3、4、5等。如果删除了JobSeekerProfile记录,那么我有一个问题,可以说在我的表中我有记录id是1-10,然后是15,然后是29,因为我要删除用户的个人资料并进行测试,因此,它为某些用户显示了错误的个人资料。我已经设法解决了这个问题,但是现在我的下一个路线或按钮不再起作用,它也没有移至job_seekers_profiles表中的下一个ID。

这是我的控制器AdminEmployerSearchController.php中修改后的代码:

class AdminEmployerSearchController extends Controller
{
    public function show($id)
    {
        $user = Auth::user();

        $jobSeekerProfiles = JobSeekerProfile::where('id', 1)->get();

        $employerProfile = EmployerProfile::all()->where('id', $user->id)->first();

        $jobSeekerProfileNewId = JobSeekerProfile::all()->where('id', $id);

        $video1 = Video::all()->where('id', $jobSeekerProfiles[0]->video_one_id)->first();;

        $video2 = Video::all()->where('id', $jobSeekerProfiles[0]->video_two_id)->first();;

        $video3 = Video::all()->where('id', $jobSeekerProfiles[0]->video_three_id)->first();;

        return view('admin.employer.search.show', compact('jobSeekerProfiles', 'employerProfile', 'video1', 'video2', 'video3', 'jobSeekerProfileNewId'));
    }

    public function next($id)
    {

        $jobSeekerProfiles = JobSeekerProfile::skip($id)->take($id)->limit(1)->get();

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

修改后的刀片文件:

<div class="row">
    <div class="col-md-12">
        <h1>My Profile</h1>
        <video width="100%" height="100%" controls style="margin-top: 0;">
            {{--                <source src="highrjobs/storage/app/public/videos/{{ $videos[0] ? $videos[0]->file : '' }}" type="video/mp4">--}}
            <source src="http://highrjobs.test/storage/app/public{{ $video1->file ?? '' }}" type="video/mp4">
            Your browser does not support the video tag.
        </video>
    </div>
    <div class="col-md-6">
        <br><br><br>
        <h1 class="h1-admin">{{$jobSeekerProfileNewId->first()->first_name}} {{$jobSeekerProfileNewId->first()->last_name}}</h1>
    </div>
    <div class="col-md-6 text-right">
        <br><br><br>
        <video width="100" height="auto" controls>
            <source src="http://highrjobs.test/storage/app/public{{ $video2->file ?? '' }}" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        <video width="100" height="auto" controls>
            <source src="http://highrjobs.test/storage/app/public{{ $video3->file ?? '' }}" type="video/mp4">
            Your browser does not support the video tag.
        </video>
    </div>
    <div class="col-md-12">
        <h3>Experience:</h3>
        <p>{{$jobSeekerProfileNewId->first()->experience}}</p>
        <h3>Additional Skills:</h3>
        <p>{{$jobSeekerProfileNewId->first()->additional_skills}}</p>
    </div>
    @if(Auth::user()->role_id === 2)
        <div class="col-md-6">
            <a href="" type="button" class="btn btn-lg btn-block btn-danger">Skip</a>
        </div>
        <div class="col-md-6">
            <button type="button" class="btn btn-lg btn-block btn-success" data-toggle="modal" data-target="#exampleModal">Request an Interview</button>
            <br><br>
        </div>
    @endif
</div>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content" style="height: 340px;">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Request an Interview</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="col-md-12">
                    <p>Select 2 options for your availabilities:</p>
                </div>
                <div class="col-md-12" style="display: inline-flex;">

                    {!! Form::open(['method'=>'POST', 'action'=>'AdminEmployerInterviewRequestsController@store', 'files'=>true, 'style'=>'width: 100%;']) !!}

                    <div class="form-group">
                        <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                            {!! Form::text('date_time1', null, ['class'=> $errors->first('date_time1') ? 'border-danger form-control datetimepicker-input' : 'form-control datetimepicker-input', 'data-target'=>'#datetimepicker1']) !!}
                            <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                                <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                            </div><br>
                        </div>
                        <small class="text-danger">{{ $errors->first('date_time1') }}</small>
                    </div>
                    <div class="col">

                    </div>

                    <div class="form-group">
                        <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
                            {!! Form::text('date_time2', null, ['class'=> $errors->first('date_time2') ? 'border-danger form-control datetimepicker-input' : 'form-control datetimepicker-input', 'data-target'=>'#datetimepicker2']) !!}
                            <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
                                <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                            </div><br>
                        </div>
                        <small class="text-danger">{{ $errors->first('date_time2') }}</small>
                    </div>

                    <div class="form-group">
                        {!! Form::hidden('user_id', Auth::user()->id, ['class'=>'form-control']) !!}
                    </div>

                    <div class="form-group">
                        {!! Form::hidden('job_seeker_profile_user_id', $jobSeekerProfileNewId->first()->id, ['class'=>'form-control']) !!}
                    </div>

                    <div class="form-group">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        {!! Form::submit('Send Interview Request', ['class'=>'btn btn-primary float-right']) !!}
                    </div>
                    <br><br><br><br>

                    {!! Form::close() !!}

                </div>
            </div>

        </div>
    </div>
</div>

为什么控制器中的下一个方法不再起作用?


我的数据库中有一个名为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,然后再使用该ID,但似乎无法正常工作。下面是页面的屏幕截图,您将明白我的意思。 enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

将跳过第2条记录返回第3个结果。

答案 1 :(得分:0)

您需要使用分页来实现:

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

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