Hi I made a Laravel Project which I can view some records with sorting. For examle I have a deliveries page with URL
http://localhost/dboard/public/deliverytracker
In that form there are From Date and To Date to sort out the data before the query.
When I clicked submit the data is rendered and the pagination links is there. However when click, for example page 2 of the pagination which is
http://localhost/dboard/public/deliverytracker?page=2
in the link it won't rendered the page 2 of the data. It just reloaded the page where I need to select again the from date and to date then clicking submit again. Did I missed something?
Here's my route
/* Delivery Tracker */
Route::get('deliverytracker', 'DeliveryTrackerController#deliveryIndex');
Route::post('deliverytracker', 'DeliveryTrackerController#getDeliveries');
My function when I submit the form
public function getDeliveries()
{
$rules = array(
'fromdate' => 'required',
'todate' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
// Check if all fields is filled
if ($validator->fails())
{
return Redirect::to('deliverytracker')->withErrors($validator);
}
else
{
$from = Input::get('fromdate');
$to = Input::get('todate');
$deliveries = new DeliveryTracking();
$result = $deliveries->getDeliveries($from, $to);
// Get the current page from the url if it's not set default to 1
$page = Input::get('page', 1);
// Number of items per page
$perPage = 5;
// Start displaying items from this number;
$offSet = ($page * $perPage) - $perPage; // Start displaying items from this number
// Get only the items you need using array_slice (only get 10 items since that's what you need)
$itemsForCurrentPage = array_slice($result, $offSet, $perPage, true);
// Return the paginator with only 10 items but with the count of all items and set the it on the correct page
$result = new LengthAwarePaginator($itemsForCurrentPage, count($result), $perPage, $page);
$result->setPath('deliverytracker');
return view('deliverytracker.index')->with(array('result' => $result));
}
}
My view
<div class="panel-body">
#if(isset($result))
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Delivery Status</td>
<td>Ref. Number</td>
<td>PO #</td>
<td>Count</td>
<td>Delivery QTY</td>
<td>Date Delivered</td>
<td>Filename</td>
<td>Invoice #</td>
<td>Date Invoice</td>
</tr>
</thead>
<tbody>
#foreach($result as $key => $value)
#if($value->stat == "Completed")
<tr>
<td class="bg-success">{{ $value->stat }}</td>
<td class="bg-success">{{ $value->oc }}</td>
<td class="bg-success">{{ $value->pon }}</td>
<td class="bg-success">{{ $value->cnt }}</td>
<td class="bg-success">{{ $value->dq }}</td>
<td class="bg-success">{{ $value->dd }}</td>
<td class="bg-success">{{ $value->fn }}</td>
<td class="bg-success">{{ $value->inum }}</td>
<td class="bg-success">{{ $value->di }}</td>
</tr>
#elseif($value->stat == "Active")
<tr>
<td class="bg-danger">{{ $value->stat }}</td>
<td class="bg-danger">{{ $value->oc }}</td>
<td class="bg-danger">{{ $value->pon }}</td>
<td class="bg-danger">{{ $value->cnt }}</td>
<td class="bg-danger">{{ $value->dq }}</td>
<td class="bg-danger">{{ $value->dd }}</td>
<td class="bg-danger">{{ $value->fn }}</td>
<td class="bg-danger">{{ $value->inum }}</td>
<td class="bg-danger">{{ $value->di }}</td>
</tr>
#endif
#endforeach
</tbody>
</table>
{!! $result->render() !!}
#else
No records found.
#endif
</div>
In your post you mention that the second page is called using only ?page=N, seeing your route is defined with both GET and POST you will hit the route definition for GET when clicking the next page link.
The pages buttons are simply links and not POST forms. So instead of hitting the getDeliveries() you are hitting deliveryIndex().
Also you might consider appending the fromdate and todate to your pagination links; see Appending To Pagination Links
Form fields(fromdate, todate) are not automatically attached to the pagination. You need to add query string values to paginator using ->append() method.
// Return the paginator with only 10 items but with the count of all items and set the it on the correct page
$result = new LengthAwarePaginator($itemsForCurrentPage, count($result), $perPage, $page);
$result->setPath('deliverytracker');
$result->appends(['fromdate' => $from, 'todate' => $todate]);
Reference:
http://laravel.com/api/5.0/Illuminate/Contracts/Pagination/Paginator.html#method_appends
Related
In my controller I've this method:
public function code($code)
{
$user = Auth::user();
$instituteCodes = DB::table('institute_codes')->where(['code' => $code, 'institute_id' => Auth::id()]);
if($instituteCodes->exists()){
$claim = DB::table('code_claims')->where('institute_code_id', $instituteCodes->value('id'))->get();
$allusers = collect($claim);
$allusers->values()->all();
$course = Course::findOrFail($instituteCodes->value('course_id'));
return view('institute.code', compact(['allusers', 'course']));
}
}
institute_codes table
id
code
course_id
1
ABC
1
2
ZXC
5
course table
id
name
1
Python
2
Laravel
I've a route which passes $code parameter to public function code() I'm getting same course data on using different codes in blade view but when I use return $course or dd($course) in controller it returns the the expected result i.e. different courses for different codes. But in blade I'm getting same courses. Any help will be appreciated.
This is happening!
Why both codes are returning same course in blade and different courses(this is what I want) on using dd or return.
Edit 1
Blade View
<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th class="long-table-row">Email</th>
<th>Code</th>
<th>Course</th>
<th>Language</th>
<th>Enrolled On</th>
<th>Progress</th>
<th>Certificate</th>
<th>Action</th>
</tr>
</thead>
#foreach ($allusers as $item)
<tr>
<td class="long-table-row">
{{ $item->user_email }}
</td>
<td class="uppercase">{{ $code }}</td>
<td>{{ $course->c_name }}</td>
<td>{{ $course->language->name }}</td>
<td>29/07/2022</td>
<td>100%</td>
<td><i class="bi bi-check-circle-fill" style="font-size: 1.2vw; color: rgb(0, 200, 90);"></i></td>
<td></td>
</tr>
#endforeach
<tbody>
</table>
</div>
I'm getting same c_name and language name for different codes.
I think you are iterating through the allusers but for course you are only picking the first course in the foreach loop of allusers.
something really strange is happening. So I have a table named Lactinfo_News with 1 row. I've created also a view called LACTINFO_VW_LatestNews which have "SELECT * FROM Lactinfo_News" and is returning the same 1 row.
I'm using Eloquent and In my news Manager I have,
public function GetLatestNews($rowsPerPage) {
$list = DB::table('LACTINFO_VW_LatestNews')
->orderBy('RegistedDate', 'DESC')
->paginate($rowsPerPage);
return $list;
}
where $rowsPerPage = 30.
In my controller I have,
// >> current page
$page = '1';
if (! empty ( $request->query ( 'page' ) )) {
$page = $request->query ( 'page' );
}
// >> search
$nM = new NewsManager();
$list = $nM->GetLatestNews($page, $this->nbOfRowsPage);
return view ('admin.news.index', [
'results' => compact($list),
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
}
And in my view,
<table id="news-results" class="hover responsive" style="margin-top: 20px;">
<thead>
<tr>
<th scope="column">Título</th>
<th scope="column">Descrição</th>
<th scope="column">Data Início</th>
<th scope="column">Ficheiro</th>
<th scope="column">Registada em</th>
<th scope="column">Criada por</th>
</tr>
</thead>
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->title }}</td>
<td>{{ $r->description }}</td>
<td>{{ Carbon\Carbon::parse($r->startDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ $r->registedDate }}</td>
<td>{{ $r->createdBy }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>
</table>
If I dump the $results var is empty..
What's happening? This is really strange :S I don't think I need to execute any artisan command because I've add a new function to my manager but I'm stuck with this problem for a few days...
You are using compact on an object of type LengthAwarePaginator, which is kind of weird IMHO.
Why don't you pass $list in your view?
My problem was in the Controller and in my View,
So I've changed my controller return to
return view ('admin.news.index', [
'results' => $list,
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
And in my view I wasnt calling in a wrong way my parameters...
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->Title }}</td>
<td>{{ $r->Description }}</td>
<td>{{ Carbon\Carbon::parse($r->StartDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ Carbon\Carbon::parse($r->RegistedDate)->format('d/m/Y') }}</td>
<td>{{ $r->refCredential }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>
I'm sorry, I have a problem with my laravel pagination Gives me one by one page for example, in my table 16 row I make paginate (10), the pagination gives me in the first page from 1 to 10 and in the second page from 1 to 6 I want the normal pagination from 1 to 10 and from 11 to 16 any help, please
public function allappointmnts(){
$allapo=DB::table('bookappoitments')->orderBy('times.id')
->join('users','bookappoitments.users_id','users.id')
->join('times','bookappoitments.times_id','times.id')
->join('dates','bookappoitments.Dates_id','dates.id')
->paginate(10);
return view('admin.Managers.allappoinments',compact('allapo'));
}
in the blade page:
<div class="text-center">
{!! $allapo->links(); !!}
</div>
The table :
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td>{{ $loop->index+1 }}</td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td>
<a class="btn btn-success btn-mini deleteRecord " href="{{url('showbymanagment',$Appointments->id)}}">Show</a>
</td>
</tr>
#endforeach
Your problem is that you're using the current index of each loop - which will always be within the range of 1-10 of each page (since you have 10 elements per page).
You need to get the current page you're on using
$allapo->currentPage()
Then get the number of elements per page you got, using
$allapo->perPage()
Multiply these two, and it will be the base number for your pagination, meaning that its this number you should increment from.
The indexed table should then be
{{ $allapo->currentPage() * $allapo->perPage() + $loop->index }}
instead of
{{ $loop->index+1 }}
Then, to fix the heading-numbers (10 and 6 at the top), get the total number of results instead of the count of the page using
$allapo->total()
See the Laravel documentation for more details.
Not entirely sure, but you will need to write some sort of function to fix this.
Essentially you need to do this:
Get the page number (ex: 1)
Multiply it by the rows per page (ex: 10)
Add the iteration number
Subtract the per page number
function correct_pagination_numbers($cp, $pp, $counter)
{
$c = (($pp * $cp) + $counter) - $pp;
return $c;
}
Then you can call the function in your view like so:
<td>{{ correct_pagination_numbers($allapo->currentPage(), $allapo->perPage(), $loop->index) }}</td>
It would probably be the best solution to do this in a helper file. If you don't know how to create a helper file and autoload it, you can look here.
Try this code:
<?php $i = $lists->perPage() * ($lists->currentPage() - 1); ?>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td><?php $i++; ?>{{ $i }} </td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td><a class="btn btn-success btn-mini deleteRecord " href="
{{url('showbymanagment',$Appointments->id)}}">Show</a></td>
</tr>
#endforeach
Try ths
#foreach($allapo as $key => $Appointments)
{{ $key + $allapo->firstItem() }}
#endforeach
to see the correct number change the line
<td>{{ $loop->index+1 }}</td>
to:
<td>{{ ($allapo->currentPage() - 1) * $allapo->perPage() + $loop->iteration }} <td>
I am developing a project in laravel 5.3. where I am using a users table and a points table in database. users are associated with transactions of points. these transections are recorded in points table. my points table looks like in my previous question who's link is this
-- I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their --
. Now I am fetching users' all data in a page whare I want to show the net balance of there points too. you can see the result in image.image is here
so currently I am using the following code in controller.
public function users_list()
{
$ptitle = "Website Users";
$pdesc = "";
//$total_users = User::count();
$users = User::select('*')->orderby('created_at', 'desc')->get();
return view('admin.all-users-list',
['ptitle' => $ptitle,
'pdesc' => $pdesc,
'users' => $users,
'total_users' => $this->total_users,
]);
}
And getting result in view like following
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td></td>
</tr>
<?php }?>
</tbody></table>
But I dont know how can i create a join in query builder to fetch net points for every user in list
Just started to working with laravel and found it really cool framework to work with , I'm working with basic Shopping cart and after i do Add to Cart Option , product values goto another table called "customer_items" along with current user ID,
now when I want to call current product which user already added to cart and show it in a table ,
here is the code
#foreach($shoppingCarts as $items)
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total"> <img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
note : {{ $items->product_price }} , usually there is 4, 6 items added by user ,
I want to get total value of product price from each row ,
Thanks in Advance for the help :) !
Used this and worked fine ,
<?php $total = 0; ?>
#foreach($shoppingCarts as $items)
<?php $total += $items->product_price; ?>
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total">{{ $total }}<img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
or
#foreach($shoppingCarts as $items)
<tr>
<td class="name">{{ $items->product_name }}</td>
<td class="price">{{ $items->product_price }}</td>
<td class="total">{{ $items->sum('product_price') }}<img class="tooltip-test" data-original-title="Remove" src="img/remove.png" alt=""></td>
</tr>
#endforeach
Thanks everyone for the support :) !
Source : https://laracasts.com/discuss/channels/general-discussion/get-total-value-from-table-rows