Laravel 5.2 pagination issue with select box - php

I would like to display data based on the chosen values from select box and search box. but select box ( maybe search box has same issue too) get reset whenever I click the page 2,3,4....
I believe I missed something with pagination code in the view / controller.
Please let me know the way to solve this issue.
Controller
class MemberListTestController extends Controller
{
public function search(Request $request)
{
$request->flash();
$memberName = $request->input('memberName');
$chosenCenter = $request->input('chosenCenter');
$memberLists= DB::table('tbMbrMember as Member')
->select(['Org.orgNm',
'Member.firstNm',
'Member.lastNm',
'Member.regDate',
'MshipCd.mshipNm',
'Att.dt',
'Member.phoneCell',
'Member.email',
'Group.groupNm',
DB::raw('MAX(Att.dt) AttDt, GREATEST(MAX(Reg.endDt),MAX(Reg.expDt)) rExpDt')])
->join('tbMbrCenter as Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->leftjoin('tbMbrGroup as Group','Center.orgCd','=','Group.orgCd')
->leftjoin('tbMbrMshipReg as Reg', 'Member.mbrCd', '=', 'Reg.mbrCd')
->leftjoin('tbCmMshipCd as MshipCd', 'MshipCd.mshipCd', '=', 'Reg.mshipCd')
->leftjoin('tbMbrAtt as Att', 'Att.mbrCd', '=', 'Member.mbrCd')
->where('Center.orgCd', $chosenCenter)
->groupby('Member.mbrCd')
->paginate(25);
$centerLists = DB::table('tbMbrCenter as Center')
->select('Org.orgNm','Center.orgCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->where('Center.isShow','1')
->where('Org.companyCd','c1')
->where('Org.isShowList','1')
->groupby('Center.orgCd')
->orderby('Org.orgNm')
->get();
return view('member.memberList_test')
-> with('memberLists', $memberLists)
-> with('centerLists', $centerLists);
}
}
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('member/test','MemberListTestController#search');
Route::post('member/test','MemberListTestController#search');
});
View
#extends('layouts.master')
#section('content')
<div class="container-fluid">
<p></p>
<div class="row"></div>
<div class="row">
<div class="col-md-4">
<form method="post" action="{{ url('member/test') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" id="chosenCenter" name="chosenCenter">
<option value="" selected disabled> Choose Center</option>
#foreach ( $centerLists as $centerList )
<option value= {{ $centerList->orgCd }} #if (old('chosenCenter') == $centerList->orgCd) selected #endif > {{ $centerList->orgNm }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control"
placeholder="Search for First Name, Last Name... 'search all' for blank "
name="memberName">
<span class="input-group-btn">
<button class="btn btn-default " type="submit">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class='row'></div>
<div class="row">
<table class="table table-striped table-condensed">
<thead>
<th>#</th>
<th>Center</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Reg.</th>
<th>Recent Membership</th>
<th>Date of Membership Exp.</th>
<th>Latest Attendance</th>
<th>Cell Phone</th>
<th>Email</th>
<th>Member Group</th>
</thead>
<tbody>
<?php $count = 1; ?>
#foreach($memberLists as $memberList)
<tr>
<td>{{ (($memberLists->currentPage() - 1 ) * $memberLists->perPage() ) + $count++}}</td>
<td> {{ $memberList->orgNm }}</td>
<td> {{ $memberList->firstNm }} </td>
<td> {{ $memberList->lastNm }} </td>
<td> {{ $memberList->regDate }} </td>
<td> {{ $memberList->mshipNm }}</td>
#if (($memberList->rExpDt) >= date('y-m-d'))
<td> {{$memberList->rExpDt }}</td>
#else
<td> Expired on {{ $memberList->rExpDt }} </td>
#endif
<td>{{ $memberList->AttDt }}</td>
<td> {{ $memberList->phoneCell }}</td>
<td> {{ $memberList->email }}</td>
<td> {{ $memberList->groupNm }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $memberLists->render() !!}
</div>
</div>
#stop

You have to pass the parameters via GET and recover the parameters from the URL.
{!! $memberLists->appends(Input::all())->render() !!}
This will send you to page 2 and so on... with all the inputs values.

I found a video in laracasts that explains all the built-in pagination options for your blade template in laravel 5.2 :
https://laracasts.com/lessons/crazy-simple-pagination
you will see that the correct way for a search page is:
{{ $memberLists->appends(Request::except('page'))->links() }}
This way it will keep the current url parameteres.

Related

How do i calculate and view total work_time for specific id in laravel in one row by date from to?

I have two tables employes and attendances . What I want is to sum work_time for each employe id and show in my view.
employes Table id attendances Table id employe_id work_time
I want to sum work_time column for specific employe id by date from to?. I am new to laravel so how do I do that with laravel eloquent.
Employe.php
class Employe extends Model
{
use HasFactory;
protected $fillable=['id','employe_code','employe_name','workplace'];
public function attendances(){
return $this->hasMany('App\Models\Attendance');
}
}
Attendance.php
class Attendance extends Model
{
use HasFactory;
protected $fillable=['id','employe_id','attendence_date','clock_in','clock_out','work_time','attendence_status'];
public function employe()
{
return $this->belongsTo('App\Models\employe', 'employe_id');
}
AttendanceController.php
public function attendanceReport(){
$empname = Employe::get();
return view('pages.Attendance.attendance_report', compact('empname'));
}
public function attendanceSearch(Request $request){
$empname = Employe::get();
if($request->employe_id == 0){
$emp = Attendance::whereBetween('attendence_date', [$request->from, $request->to])->get();
return view('pages.Attendance.attendance_report',compact('emp','empname'));
}
else{
$emp = Attendance::whereBetween('attendence_date', [$request->from, $request->to])
->where('employe_id',$request->employe_id)->get();
return view('pages.Attendance.attendance_report',compact('emp','empname'));
}
}
attendance_report.blade.php
<form method="post" action="{{ route('attendance.search') }}" autocomplete="off" id="sh11">
#csrf
<div class="row">
<div class="col-md-3">
<div class="form-group">
<select class="custom-select mr-sm-2"name="employe_id">
<option value="0">all</option>
#foreach($empname as $empname)
<option value="{{ $empname->id }}">{{$empname->employe_name }}</option>
#endforeach
</select>
</div>
</div>
<div class="card-body datepicker-form">
<div class="input-group" data-date-format="yyyy-mm-dd">
<span class="input-group-addon">Date</span>
<input type="text" class="form-control range-from date-picker-default" value={{ date('Y-m-d') }} required name="from">
<span class="input-group-addon">to date</span>
<input class="form-control range-to date-picker-default" value={{ date('Y-m-d') }} type="text" required name="to">
</div>
</div>
</div>
<button class="btn btn-success btn-sm nextBtn btn-lg pull-right" type="submit">{{trans('Students_trans.submit')}}</button>
</form>
#isset($emp)
<div class="table-responsive">
<table id="datatable" class="table table-hover table-sm table-bordered p-0" data-page-length="50"
style="text-align: center">
<thead>
<tr>
<th class="alert-success">#</th>
<th class="alert-success">code</th>
<th class="alert-success">name</th>
<th class="alert-success">work_time</th>
<th class="alert-success">attendence_date</th>
<th class="alert-warning">###</th>
</tr>
</thead>
<tbody>
#foreach($emp as $EMP)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $EMP->employe->employe_code }}</td>
<td>{{ $EMP->employe->employe_name }}</td>
<td>Hour {{ $EMP->work_time }} </td>
<td>{{ $EMP->attendence_date }}</td>
<td>
#if($EMP->attendence_status == 0)
<span class="btn-danger">Presence</span>
#else
<span class="btn-success">Absence</span>
#endif
</td>
</tr>
#endforeach
</table>
</div>
#endisset
you can try to add selectRaw to your releation
public function attendances(){
return $this->hasMany('App\Models\Attendance')->selectRaw('SUM(work_time) as total_work_time');
}
check the decumentation

Get the value of table data from the same row

I'm trying to explain my question the best I can, so sorry if it's a bit confusing.
I have in my HTML Table, with table row, and table data, each row is filled with data from data base, and the table is automatically generated.
Example is below.
<thead>
<tr>
<th>Place ID</th>
<th>Map Name</th>
<th>Place Name</th>
<th>Price 1</th>
<th>Price 2</th>
<th>Price 3</th>
<th>Price 4</th>
#if (Auth::user()->role == "admin")
<th>Action</th>
<th>Booking</th>
#endif
</tr>
</thead>
<div id="preloader"></div>
<tbody id="hidden_table_processing">
#foreach ($places as $place)
<tr>
<td>{{ $place->place_id }}</td>
<td>{{ $place->map_name }}</td>
<td>{{ $place->place_name }}</td>
<td>{{ $place->price1 }} €</td>
<td>{{ $place->price2 }} €</td>
<td>{{ $place->price3 }} €</td>
<td>{{ $place->price4 }} €</td>
For booking table head, its filled with a quick booking button, that is supposed to get the Place ID of the data from the same raw, and shows it inside a popup, bellow is the code that creates the popup and fills the data.
<td>{{-- status print --}}
#if ($place->status==0)
<!-- Trigger the modal with a button -->
Quick Book
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<form method=POST action="{{ route('admin.place.quickbooking') }}">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Quick Book</h4>
</div>
<div class="modal-body">
#csrf
<div class="form-group">
<label for="usr">ID:</label>
<input type="text" class="form-control" value="{{ $place->place_id }}" id="qBookingID" name="qID" readonly>
<label for="usr">Full Name:</label>
<input type="text" class="form-control" value="{{ Auth::user()->name }}" id="qBookingFullName" name="qFullName">
<label for="usr">Number of Guests:</label>
<input type="number" class="form-control" id="qBookingNumberOfGuests" name="qNumberOfGuests">
<label for="usr">Number of Babies:</label>
<input type="number" class="form-control" id="qBookingNumberOfBabies" name="qNumberOfBabies">
<label for="usr">Check In:</label>
<input type="text" class="form-control" value="{{ session()->get('startingRange') }}" id="qBookingStartDate" name="qStartDate" readonly>
<label for="usr">Check Out:</label>
<input type="text" class="form-control" value="{{ session()->get('endingRange') }}" id="qBookingEndDate" name="qEndDate" readonly><br>
<label for="usr">Map Name: {{ $place->map_name }}</label><br>
<label for="usr" id="qBookingCreatorName" name="qCreatorName">Booked By: {{ Auth::user()->name }}</label>
</div>
{{-- Book --}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" style="color: red; border: 1px solid red;" >Close</button>
<button type="submit" class="btn btn-default" style="color: green; border: 1px solid green;" type="submit">Book</button>
</div>
</div>
</form>
</div>
</div>
#endif
#if ($place->status==-1)
<p>Disabled</p>
#endif
#if ($place->status==2)
<p style="color:red;">Busy</p>
#endif
</td>
My problem is that the auto filled ID is only being filled by the First Data, as no matter which row I click on, the ID stays the same "the first ID from the table"
Any idea what might be causing this, or am I missing something very obvious?
THank you for your help.
For your case the better choice is pass the object place to a function using javascript, every time that you give click the modal appears with the data
that you want to edit or show.
Edit
#push('script')
<script>
$(document).ready(function () {
$('body').on('click', '#editModal', function (event) {
event.preventDefault();
let id = $(this).data('id');
console.log(id)
let name = {{ Auth::user()->name }}
let startdate = {{ session()->get('startingRange') }}
let enddate = {{ session()->get('endingRange') }}
let bookname = 'Booked By:' + name
$.get('place/' + id + '/edit', function (data) {
$("#qBookingID").val(data.data.place_id);
$(#qBookingFullName").val(name);
$("#qBookingStartDate").val(startdate);
$("#qBookingEndDate").val(enddate);
$("#qBookingCreatorName").val(bookname);
$('#Mymodal').modal('show');
})
});
});
</script>
#endpush

"Class App\Http\Controllers\Frontend\FrontendController does not exist"

I installed a laravel script. but a page not worked and appear
Trying to get property 'name' of non-object (View:
/mylocal/core/resources/views/admin/users/servicePrice.blade.php)
for this line:
<th>{{ $item->service->name }}</th>
this is the file codes:
<div class="tile">
#include('admin.layouts.flash')
<h3 class="tile-title">Service Price Details</h3>
<form method="post" action="{{ route('store.service.price', $user_id) }}">
#csrf
#method('PUT')
<table class="table table-hover">
<tbody>
#foreach($items as $item)
<tr>
<th>{{ $item->category->name }}</th>
<th>{{ $item->service->name }}</th>
<td>
<div class="input-group">
<input type="hidden" name="id[]" value="{{ $item->id }}">
<input class="form-control" type="text" name="price[]" value="{{ $item->price }}">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</td>
</tr>
#endforeach
</table>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
Check your relation return a object or not.
<th>
#if($item->service)
{{ $item->service->name }}
#endif
</th>
You didn't share the content of array $items.Please make sure you have a service array in $items array and also make sure the name variable exists in service array.
and can you show me the result of this:
var_dump($items);

Undefined variable: user_id in Laravel view

The Routes
Route::get('/adminContacts/{user_id}', 'AdminContactsController#index')->name('adminContacts.index')->middleware('is_admin');
Route::get('/adminContacts/{user_id}/create', 'AdminContactsController#create')->name('adminContacts.create')->middleware('is_admin');
adminContactController
public function index($user_id)
{
// Confirm User exists
User::findOrFail($user_id);
$filter = request('filter', NULL);
$contacts = NULL;
if ($filter == NULL)
$contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
else
$contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
->orWhere('number', 'like', '%'.$filter.'%')
->sortable()->paginate(5);
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($user_id)
{
return view('adminContacts.create')->withUserId($user_id);
}
index.blade.php
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index') }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
The error is:
Undefined variable: user_id (View: D:\laragon\www\SampleContacts\resources\views\adminContacts\index.blade.php)
Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set
$contact->owner_id = $user_id;
so the entered contact apears under that users table.
I don't think you can pass variables into your view like you're doing now.
In your controller, try changing this line:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
To this:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
You could explicitly name the variable when passing it to the view, either this way
return view('adminContacts.index')->with('user_id', $user_id);
Or
return view('adminContract.index', compact('user_id'));
I'm not sure what the name of the variable becomes when passed to the view via withUserId, but I'm guessing this is the issue.
Found the problem.
#extends('layouts.app')
#section('content')
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index', $user_id) }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
#endsection
Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'
Thank you for all the help.

Trying to get property of non-object on Laravel 5

Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:

Categories