Laravel pagination add page number - php

Am sending a get request and adding the pagination value but now i would like to attach the page
So in my URL I have
http://localhost:8080/users?paginator=10&page=1
The value of page and paginator are controlled from frontend(angular4)
So in my controller I have
class UserController extends Controller{
public function index(){
return User::paginate($request->paginator);
}
}
The above works for the paginator value but now how do I attach the page number?

return User::paginate($request->paginator, ['*'], 'page', $request->page);
The default paginate method takes the following parameters.
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null);
For further details, see this link.

From the docs here:
By default, the current page is detected by the value of the page query string argument on the HTTP request.

Related

Laravel empty route parameter

I have multiple routes which will directed to the same controller and method. I want the second route will have an empty customParams, but the first one will use the custom params. What should i do? Thanks
Route::get('{customParams?}/{slug}/{registrationCode}/detail', [SubmissionController::class, 'submissionDetail'])->name('submission.detail');
public function submissionDetail($customParams = '', $slug, $registrationCode)
{
//
}
First route was running perfectly
Detail
Second route did not work and produce 404 page
Detail
Make 2 routes for it, because in your case first, param is your optional.
in web.php
Route::get('{customParams?}/{slug}/{registrationCode}/detail', 'HomeController#details');
Route::get('{slug}/{registrationCode}/detail', 'HomeController#detail'); // add new route with different function name
in controller
/* your default function will call when your customParams having some value */
public function details($customParams = null, $slug, $registrationCode) {
dd($customParams, $slug, $registrationCode);
}
/* when your customParams will empty which is your 2nd case */
public function detail($slug, $registrationCode) {
$this->details(null, $slug, $registrationCode);
}
So as a result...
when URL calls with this http://127.0.0.1:8000/welcome/slug/r-code/detail
when URL calls with http://127.0.0.1:8000/slug/r-code/detail

How to pass post parameters in laravel from the route to the controller?

I'm doing this query in the payment controller and i need to get a post request from the route.
Controller:
class PaymentController extends Controller
{
public function apiPaymentByUserId($date_from, $date_to) {
$payments = DB::table("casefiles, payments")
->select("payments.*")
->where("casefiles.id", "=", 'payments.casefile_id')
->where("casefiles.user_id", "=", Auth::id())
->where("payments.created_at", ">=", $data_from)
->where("payments.updated_at", "<=", $data_to)
->get();
return response()->json([
'success' => true,
'response' => $payments
]);
}
}
Route:
Route::post('/payments/{date_from}/{date_to}', 'Api\PaymentController#apiPaymentByUserId');
How to pass multiple parameters in this post route? Thank you
For post request no need to pass param in url .You will get in request
So route will be
Route::post('/payments', 'Api\PaymentController#apiPaymentByUserId');
and controller method
public function apiPaymentByUserId(Request $request)
{
$date_from = $request->date_from;
$date_to = $request->date_to;
}
If you do not want to change your url, try this in your controller apiPaymentByUserId() method, inject the Request object along with the other path variables like like:
public function apiPaymentByUserId(Illuminate\Http\Request $request, $date_from, $date_to) {
// ... you can access the request body with the methods available in $request object depending on your needs.
}
For POST request no need to pass param in url . Send the Dates as FORM values sent via POST method along with the rest of the FORM values (if any, you're already POSTing in the FORM). You will get all FORM values sent via POST method in Request $request object instance, passed in Controller/Method.
So route will be:
Route::post('/payments', 'Api\PaymentController#apiPaymentByUserId');
and controller method:
public function apiPaymentByUserId(Request $request)
{
$date_from = $request->date_from;
$date_to = $request->date_to;
}

Laravel 7 Binding Routes

I have this route declared on laravel:
Route::get('pages/{page}/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
This route works fine and works if you make requests to:
https://example.com/pages/1/test-page
https://example.com/pages/2/other-page
https://example.com/pages/3/url-test
The problem is that I need a more friendly url as well as.
https://example.com/test-page
https://example.com/other-page
https://example.com/url-test
I want remove the suffix called pages, The numbers for the pages will never change and will be static for each one.
I've tried to make static routes for each one but can't get it to work.
Route::get('other-page', array('as' => 'other-page', function() {
return App::make('Common\Pages\CustomPageController')->show(2);
}))->middleware(['web', 'prerenderIfCrawler']);
I would appreciate a little help.
You could always get the URL segment in the Controller and use that to know what page you are on. If you don't want to do that you could pass extra information in the 'action' to specify the page:
Route::middleware(['web', 'prerenderIfCrawler'])->group(function () {
Route::get('test-page', [
'uses' => 'Common\Pages\CustomPageController#show',
'page' => 'test-page',
]);
...
});
Then you can get this extra information in the Controller:
public function show(Request $request)
{
$page = $request->route()->getAction('page');
...
}
If you knew all the pages you can use a route parameter with a regex constraint to restrict it to only those page names:
Route::get('{page:slug}', ...)->where('page', 'test-page|other-page|...');
public function show(Page $page)
{
...
}
You could just make use of a wildcard to catch your routes like this:
Route::get('/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
Then in your controller:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
// ...
}
Just be careful with where you place the route. It should be at the end of your routes otherwise it will catch all the request of your app.
// ...
// my other routes
// ...
Route::get('/{slug}', ...);
By the way, if you want to bind your page models using the slug attribute do this:
Route::get('/{page:slug}', 'Common\Pages\CustomPageController#show')->//...
^^^^^^^^^
Then in your controller:
public function show(Page $page)
{ ^^^^^^^^^^
// ...
}
Check this section of the docs.

New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

Is it possible to call a function in the controller without using a route or should I make a new route with two parameters as below that redirects to the specific page after the session has been added?
route::get('addsesion/{session-name}/{session slug};
If it's possible with ajax, can someone please point me in the right direction?
Basically what I would like to do is call the function addSession($session_name, $slug) from a controller with ajax on link <a href/> click , where it stores my specific session name and current page's slug.
It should call this addSession function on a click, store session data and then redirect to a different url. e.g. /seeparts, where it displays all saved session data.
Do I have to make a new route route::get('addsesion/{param1 - session-name}/{param2 - session slug}', currentController#addSession ); and then use that route as an ajax url? Or is there any other way how to use the controller's function?
My current Controller:
public function showAll() {
$parts = \DB::table() - > all();
$data = [
'parts' => $parts,
];
return view('partlist', $data);
}
public function showCpu($slug) {
// Specification query
$specs = \DB::table() - > select($select_columns) - > where('slug', $slug) - > first();
$data = [
'specs' => $specs,
'slug' => $slug
];
return view('part', $data);
}
//Add session - call this function
public function addSession($session_name, $slug) {\
Session::put($session_name, $slug);
}
}
part.blade.php:
<html>
#include('head.blade.php')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" >Add to partlist</a>
</body>
</html>
I think you can use Service Injection binding controller function in your view.
Maybe you can reference it, https://laravel.com/docs/master/blade#service-injection.
For example:
<html>
#include('head.blade.php')
#inject('currentController', 'App\Http\Controllers\currentController')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" onClick="{{ $currentController->addSession($session_name, $slug) }}">HERE</a>
</body>
</html>

pass multiple parameters to controller from route in laravel5

I want to pass multiple parameters from route to controller in laravel5.
ie,My route is ,
Route::get('quotations/pdf/{id}/{is_print}', 'QuotationController#generatePDF');
and My controller is,
public function generatePDF($id, $is_print = false) {
$data = array(
'invoice' => Invoice::findOrFail($id),
'company' => Company::firstOrFail()
);
$html = view('pdf_view.invoice', $data)->render();
if ($is_print) {
return $this->pdf->load($html)->show();
}
$this->pdf->filename($data['invoice']->invoice_number . ".pdf");
return $this->pdf->load($html)->download();
}
If user want to download PDF, the URL will be like this,
/invoices/pdf/26
If user want to print the PDF,the URL will be like this,
/invoices/pdf/26/print or /invoices/print/26
How it is possibly in laravel5?
First, the url in your route or in your example is invalid, in one place you use quotations and in the other invoices
Usually you don't want to duplicate urls to the same action but if you really need it, you need to create extra route:
Route::get('invoices/print/{id}', 'QuotationController#generatePDF2');
and add new method in your controller
public function generatePDF2($id) {
return $this->generatePDF($id, true);
}

Categories