簡體   English   中英

如何使用 Laravel 獲取表中的下一條記錄?

[英]How to get the next record in a Table using Laravel?

更新:下一步按鈕仍然不起作用。 我不得不更改一些代碼,因為我的下一個按鈕從 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 等等。它就像一個 Prev 和 Next 功能。

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 然后使用它,但我似乎無法讓它工作。 下面是頁面截圖,你會明白我的意思。 在此處輸入圖片說明

您應該使用->skip()->offset()方法定義起點,並使用->take()方法獲取數據數量

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

將返回跳過前 2 條記錄的第三個結果。

您需要使用分頁來做到這一點:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM