Get next and previous variables in Laravel view using foreach - php

I want to implement a lightbox-like gallery using bootstrap modals in my Laravel view.
I have a view that shows all posts(archive) paginated by 10 and a button to open each post on modal. What I want to achieve is having two buttons on my popup modal that can loop through my posts. I also have the jquery functionallity ready. I just dont know how to get prev and next images with foreach loop.
#extends('index')
#section('title', 'USP Archive')
#section('description', 'Archive of User Submitted Posts')
#section('featured-image', asset('/images/share/usp.jpg'))
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 {{ str_replace('.', '-', Route::currentRouteName()) }}">
<h1>USP Archive</h1>
<hr>
<div class="usp-list-archive">
#foreach ($userposts as $userpost)
<h3>Email: {{ $userpost->email }}</h3>
<br>
<p>{{ $userpost->fname }} {{ $userpost->lname }}</p>
<br>
<img src="{{asset('/images/usp/' . $userpost->image)}}" />
<br>
View
Open
<hr>
#endforeach
<div class="modal fade" id="uspModal" tabindex="-1" role="dialog" aria-labelledby="uspModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<img class="modal-image" src="" />
<button id="prev" data-src="{{ $prev }}" class="btn btn-sm btn-primary" style="position:absolute; top:0; left:0;">PREV</button>
<button id="next" data-src="{{ $next }}" class="btn btn-sm btn-primary" style="position:absolute; top:0; right:0;">NEXT</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script>
$('#uspModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var imgsrc = button.data('src')
var modal = $(this)
modal.find('.modal-image').attr("src",imgsrc);
})
$('#prev').click(function(e) {
var imgsrc = $(this).data('src')
var modal = $('#uspModal')
modal.find('.modal-image').attr("src",imgsrc);
})
$('#next').click(function(e) {
var imgsrc = $(this).data('src')
var modal = $('#uspModal')
modal.find('.modal-image').attr("src",imgsrc);
})
</script>
#endsection
As you can see I have two undefined variables $prev and $next. Those are the vars I need to define correctly to get the link of the previous and next post image source.
Thanks in advance!

#extends('index')
#section('title', 'USP Archive')
#section('description', 'Archive of User Submitted Posts')
#section('featured-image', asset('/images/share/usp.jpg'))
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 {{ str_replace('.', '-', Route::currentRouteName()) }}">
<h1>USP Archive</h1>
<hr>
<div class="usp-list-archive">
#foreach ($userposts as $index => $userpost)
<h3>Email: {{ $userpost->email }}</h3>
<br>
<p>{{ $userpost->fname }} {{ $userpost->lname }}</p>
<br>
<img src="{{asset('/images/usp/' . $userpost->image)}}" />
<br>
View
Open
<hr>
#endforeach
<div class="modal fade" id="uspModal" tabindex="-1" role="dialog" aria-labelledby="uspModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<img class="modal-image" src="" />
#if ($prev = $userposts->get($index-1))
<button id="prev" data-src="{{ $prev }}" class="btn btn-sm btn-primary" style="position:absolute; top:0; left:0;">PREV</button>
#endif
#if ($next = $userposts->get($index+1))
<button id="next" data-src="{{ $next }}" class="btn btn-sm btn-primary" style="position:absolute; top:0; right:0;">NEXT</button>
#endif
</div>
</div>
</div>
</div>
</div>
</div>

Related

Fetch data into modal popup using ajax in laravel

I am trying to fetch data from my database into a modal popup and make it dynamic... I'm also a bit confused on how to make the modal id unique for each data on the table... the "button" details pops up the modal.
This is my Table the buttons pops up a modal
This is my controller
public function show($upi)
{
$Patient = Patients::where('upi', '=', $upi)->first();
return Response::json($Patient);
}
This is my Route
Route::get('show-details/{upi}', [BssDashboardController::class, 'show'])->name('details.show');
This is my Ajax script
<script>
$(document).ready(function () {
$('body').on('click', '#show-details', function (){
var patientURL = $(this).data('url');
$.get('show-details/' + patientURL', function (d) {
$('#diagnosisModal').modal('show');
$('#date').text(data.date);
$('#age').text(data.age);
$('#gender').text(data.gender);
$('#plan').text(data.plan);
$('#authcode').text(data.authcode);
$('#findings').text(data.findings);
$('#compliants').text(data.compliants);
$('#investigation').text(data.investigation);
});
})
});
</script>
This is my Button
<a href="javascript:void(0)" id="show-details" data-url="{{ $item->upi }}">
<button class="btn-primary px-3 py-1">Details</button>
</a>
This is my modal
<div class="modal fade" id="diagnosisModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-8 pt-3">
<div class="d-flex flex-row"><label><b>Name:</b></label><div class="fw-600" id="name"></div></div>
<div class="d-flex flex-row"><label><b>DOB:</b></label><div class="fw-600" id="date">16-03-1990</div></div>
<div class="d-flex flex-row"><label><b>Age:</b></label><div class="fw-600" id="age">31 Years</div></div>
<div class="d-flex flex-row"><label><b>Sex:</b></label><div class="fw-600" id="gender">Female</div></div>
<div class="d-flex flex-row"><label><b>Plan:</b></label><div class="fw-600" id="plan">Equity</div></div>
<div class="d-flex flex-row"><label><b>Authcode:</b></label><div class="fw-600" id="authcode">-</div></div>
<div class="d-flex flex-row"><label><b>Findings:</b></label><div class="fw-600" id="findings">Pregnant</div></div>
<div class="d-flex flex-row"><label><b>Complaints:</b></label><div class="fw-600" id="compliants">ANC</div></div>
<div class="d-flex flex-row"><label><b>Investigations:</b></label><div class="fw-600" id="investigation">ANC</div></div>
</div>
<div class="col-4">
<div class="pt-4" style="width: 5rem;height: 4rem;"><img class="w-100" src="{{ asset('img/drake.jpg') }}" id="image" alt=""></div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal" style="background-color:#ff3030;border-radius:.7rem">Close</button>
</div>
</div>
</div>
</div>

How to send the correct ID to the controller with ajax?

How do I add code to the input button so that the value of the Id is sent to the Controller?
I have eight images. Then I click on the id of 116, open this image.
When I click on the biggest photo, Open a bootstrap modal for me.
This action tag form is written to this ID 120, While I have selected ID 116.
http://localhost:8000/admin/products/4/galleries/120
#section('content')
<div class="col-md-10 p-5 pt-2">
<form method="post" action="{{ route('products.galleries.store', $product->id) }}" enctype="multipart/form-data" class="dropzone" id="dropzone">
#csrf
</form>
<hr>
<div class="col-md-6 mx-auto">
<div class="row">
<div class="mx-auto" id="showImage">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="deleteImage('{{ $gallery->id }}', '{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}')">
</div>
</div>
<div class="row">
#foreach($galleries as $gallery)
<div class="col-md-2">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="showImage('{{ asset("storage/{$gallery->image}") }}', '{{ $gallery->id }}')"><br><div class="text-center text-danger">{{ $gallery->id }}</div>
</div>
#endforeach
</div>
</div>
</div>
<div class="modal fade" id="delete" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<form action="{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}" method="post">
#csrf
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">This action is not reversible.</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete the image?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</form>
</div>
</div>
#endsection
#push('script')
<script src="{{ asset('themes/js/dropzone.min.js') }}"></script>
<script>
function showImage(url, id) {
let route = "'"+'{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}'+"'";
let image = '<img src='+url+' onclick="deleteImage('+id+','+route+')" class="img-fluid cursor-pointer" />';
$('#showImage').html(image);
}
function deleteImage(id, url) {
$('#delete').modal(id, url);
}
</script>
#endpush
web.php
Route::post('products/{product}/galleries/{gallery}', 'GalleryController#delete')->name('products.galleries.delete');

How to send the to controller with ajax?

How do I add code to the input button so that the value of the Id is sent to the Controller?
I have eight images. Then I click on the id of 116, open this image.
When I click on the biggest photo, Open a bootstrap modal for me.
This action tag form is written to this ID 120, While I have selected ID 116.
http://localhost:8000/admin/products/4/galleries/120
#section('content')
<div class="col-md-10 p-5 pt-2">
<form method="post" action="{{ route('products.galleries.store', $product->id) }}" enctype="multipart/form-data" class="dropzone" id="dropzone">
#csrf
</form>
<hr>
<div class="col-md-6 mx-auto">
<div class="row">
<div class="mx-auto" id="showImage">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="deleteImage('{{ $gallery->id }}', '{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}')">
</div>
</div>
<div class="row">
#foreach($galleries as $gallery)
<div class="col-md-2">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="showImage('{{ asset("storage/{$gallery->image}") }}', '{{ $gallery->id }}')"><br><div class="text-center text-danger">{{ $gallery->id }}</div>
</div>
#endforeach
</div>
</div>
</div>
<div class="modal fade" id="delete" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<form action="{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}" method="post">
#csrf
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">This action is not reversible.</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete the image?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</form>
</div>
</div>
#endsection
#push('script')
<script src="{{ asset('themes/js/dropzone.min.js') }}"></script>
<script>
function showImage(url, id) {
let route = "'"+'{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}'+"'";
let image = '<img src='+url+' onclick="deleteImage('+id+','+route+')" class="img-fluid cursor-pointer" />';
$('#showImage').html(image);
}
function deleteImage(id, url) {
$('#delete').modal(id, url);
}
</script>
#endpush
You are looping through the gallery images, after loop iterations are done you written your code for modal, the last value of gallery which you used for modal is the last index in your collection of gallery images.
This peice of code may help you
$gallery = 5;
$galleries = [5,6,7,9,8];
foreach($galleries as $gallery) {
// code
}
// now your $gallery variable does not hold 5 instead it has 8 as you assigned its value through loop
As you used $gallery vairable before, You can fix your code like this
#foreach($galleries as $_gallery)
<div class="col-md-2">
<img src="{{ asset("storage/{$_gallery->image}") }}" class="img-fluid cursor-pointer" onclick="showImage('{{ asset("storage/{$_gallery->image}") }}', '{{ $_gallery->id }}')"><br><div class="text-center text-danger">{{ $_gallery->id }}</div>
</div>
#endforeach

my popup not showing in upper side , i have tried many ways to do thid in laravel

enter image description here
this is my laravel blade code, I want to show the popup on the same page, it is showing also. But it is not clickable, and shows fade model can anyone suggest me to do another method, as I have tried other's method also. Thank you in advance.
#foreach($popup as $pop)
<div class="popup col-md-3">
<div class="contain">
<img src="{{ $pop->front_layout }}" alt="Avatar" class="image" style="width:100%">
<div class="middle">
<a class="btn btn-warning" data-toggle="modal" data-target="#myModal">Preview</a>
</div>
</div>
{{--<div class="pops" id="{{ $pop->id }}">
{!! $pop->website !!}
</div>--}}
</div>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endforeach
Your modals don't have a unique id.
<a class="btn btn-warning"
href="#loginbutton"
data-toggle="modal"
data-target="#myModal-{{ $pop->id }}">Preview</a>
<div id="myModal-{{ $pop->id }}"
class="modal fade"
role="dialog">
As for the visualization problem you've got two possible fixes:
Add a high z-index value to your modal.
<div id="myModal-{{ $pop->id }}"
class="modal fade"
role="dialog"
style="z-index: 10000; /* adjust as needed */">
Add enough top margin to your modal to avoid having it overlap with the navbar
<div id="myModal-{{ $pop->id }}"
class="modal fade"
role="dialog"
style="margin-top: 20rem /* adjust as needed */;">
Make sure your modal is actually at the end of the page and has no parent elements besides the </body> tag
<body>
....
<div class="modal fade" ...></div>
<script ...>...</script>
<script ...>...</script>
<script ...>...</script>
</body>
try to change modal css "top" value,
Maybe this can help
.modal {
top: 120px;
}
Or change .modal-dialog css
.modal-dialog {
margin: 100px auto; }

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

UPDATE: next button still not working.
I had to change some of the code because my next button was going from id 1, to 2, 3, 4, 5 etc. If a JobSeekerProfile record was deleted, then I have an issue where let's say in my table I had records with id's 1-10 and then 15, and then 29 because I was removing people's profiles and testing things out, because of this it was showing the wrong profiles for some of the users. I've managed to fix this issue, but now my next route or button isn't working anymore, it's not moving to the next id in the job_seekers_profiles table.
Here is my modified code from my controller 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'));
}
}
Modified blade file:
<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">
Skip
</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">×</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>
Why is the next method in my controller not working anymore?
I have a Table in my database called job_seeker_profiles, where I store the profiles of those seeking a job. I have a controller called AdminEmployerSearchController, where Employers can view the profiles of the job seekers, with this show method:
public function show()
{
$jobSeekerProfiles = JobSeekerProfile::limit(1)->get();
return view('admin.employer.search.show', compact('jobSeekerProfiles'));
}
It's showing me the first record in the Table, which is what I want, to show one record at a time. I have a button in my view called 'Skip', where when clicked, I want it to show the next record in the database, right now its showing me record 1, so when clicked it would go to record 2, then record 3, then record 4 etc. It's like a Prev and Next functionality.
show.blade.php file:
#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
I thought that I would have to find the id in the Table and then work with that, but I can't seem to get it working. Below is a screenshot of the page, you'll see what I mean.
You should use ->skip() or ->offset() method to define the starting point, and ->take() method to get number of data
JobSeekerProfile::get()->offset(2)->take(1);
Will return 3rd result skipping first 2 records.
You need to use Pagination to do it:
$profiles = JobSeekerProfile::paginate(1);
return view('index', ['profiles' => $profiles]);
https://laravel.com/docs/5.8/pagination

Categories