Laravel #extends second parameter array explanation - laravel

Question:
I am new to laravel,
#extends('layouts.app', ['page' => 'Receipts', 'pageSlug' => 'receipts', 'section' => 'inventory'])
I understand #extends first parameter 'layouts.app', but what does the second parameter do?

In Laravel #extend second parameter just to pass data in another blade.
#extends('layouts.app', ['title' => 'Page Title'])
This is how the second parameter works in laravel main layout
<title>App Name - {{ ucfirst($title) }}</title>

Since you are extending layouts.blade.php file, you have to look into the file. In layouts.blade.php you need variables such as $page, $pageSlug, $section to be defined. So you are defining/passing each variable.
Like Python
page = Receipts, pageSlug = receipts, section = inventory
equals
'page' => 'Receipts', 'pageSlug' => 'receipts', 'section' => 'inventory'

Related

How do I pass all existing parameters into route blade function in Laravel? [duplicate]

I use the route-helper ({{ route('routename') }}) in my Blade template files to filter and/or sort the results of the page.
What is the easiest way to attach a parameter to the previous ones?
As an example:
I visit the page /category1 and see some products. Now I use the sorting which changes the URL to /category1?sort_by=title&sort_order=asc
If I use another filtering now, I would like the parameter to be appended to the current one. So to /category1?sort_by=title&sort_order=asc&filter_by=year&year=2017 but the result is only /category1?filter_by=year&year=2017 .
I create the Urls with the route-helper like:
route('category.show', [$category, 'sort_by' => 'title', 'sort_order' => 'asc'])
route('category.show', [$category, 'filter_by' => 'year', 'year' => $year])
You could probably use something like:
$new_parameters = ['filter_by' => 'year', 'year' => $year];
route('category.show', array_merge([$category], request()->all(), $new_parameters]);
to use all previous parameters and add new ones.
Obviously you might want to use only some of them, then instead of:
request()->all()
you can use:
request()->only('sort_by', 'sort_order', 'filter_by', 'year')

how to pass get parameters in named routes in laravel

In my index.blade.php the code is as follows:
href="/finance/reports?type=monthly&year={{ $month['year'] }}&month={{ $month['id'] }}"
and in web.php file route is defined as:
Route::get('/reports', 'ReportsController#index')->name('reports');
How can I pass the parameters in index.blade.php to make it a named route.
It is a named route already. To get a URL from the route helper for the named route with the query params appended:
route('reports', [
'type' => 'monthly',
'year' => $month['year'],
'month' => $month['id'],
]);
Would be:
http://yoursite/finance/reports?type=monthly&year=WhatEverThatIs&month=WhatEverThatWas
I'm making assumptions about your routes and that the URI you used in the example is accurate.
Define the route as:
Route::get('/finance/reports/{type}/{year}/{month}')->name('reports');
and then use it from blade template the following way:
href="{{route('reports', ['type' => 'monthly', 'year' => $month['year'], 'month' => $month['id']])}}"
See the docs for more info: https://laravel.com/docs/5.6/routing#required-parameters
Define the route as:
Route::get('/reports/{type}/{year}/{month}', 'ReportsController#index');
and use it in following way from the blade template
href="your_project_path/reports/monthly/{{ $month['year'] }}/{{ $month['id'] }}"

Laravel router with additional parameters

I have controller which called by this route website.lrv/products/{category-url}
public function categoryProducts($uri, Request $request) { /** some code **/ }
I have a few links that are responsible for ordering products, i added them directly to blade template, like this:
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'desc' ])
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'asc' ])
And the request link with ordering is:
website.com/products/chargers?order=name&by=asc
Now i want to add products filters. I have many filters, each filter can contain many values. The problem is that i don't know how to make route to get some like that:
website.com/products/chargers?width=10,20,30&height=90,120,150
or something like that.
I need to get request array like that:
$request = [
....
filters => [
width => ['10','20','30'],
height => ['90','120','150'],
],
....
];`
If you need some additional info, i will edit my question.
You can access the array you are looking for by using $request->query() within the categoryProducts function.
Edit: Also worth noting you can access them from within your blade template using the Laravel helper function request(), so {{ request()->query('width') }} would return xx where website.lrv/products/category?width=xx.
Edit 2: Note that if your query string includes commas per your example width=10,20,30 the query() will just return a comma separated string, so you would need to explode(',',$request->query('width')) in order to get the array of widths
I think you have to add them manually like this:
route('client.category.products')."?order=name&by=asc";
Try this I hope it help.

How can i access this multi URL in laravel 4?

How can i access this multi URL in laravel 4.2.
http://localhost/new/public/library/course/1/First%20Video
My code is
Route::get('library/course/{id}', function($id){
return View::make('course')->with('id',$id);
});
Route::get('library/course/{id}/{video}', function($id, $video){
$array = array('id' => '$id', 'video' => '$video');
return View::make('course')->withvideos($array);
});
You are accessing the URL correctly, and it should reach your second route.
But you have a bug in your route:
// Your code
$array = array('id' => '$id', 'video' => '$video');
You shouldn't put single quotes around your variables -- it should be:
$array = array('id' => $id, 'video' => $video);
The single quotes force PHP not to resolve the two variables, so they are simply literal strings -- not what you want here.
Also, to pass the parameters to the view, you should adjust your code to use:
return view('course', $array);

How to include Laravel Controller file like blade

First my master.blade file include menu bar using this
Include Blade file menu.blade.php
#include('menu')
But Finally I realize to send some data from db to menubar, then I create controller, Controller name is MenuController, then I create route "admin-menu". Now I want to include that link to my master blade. how to do that thank you,
To pass data to a view from either a route closure or a controller you do one of the following:
$now = \Carbon\Carbon::now();
View::make('my-view', ['name' => 'Alex', 'date' => $now]); // pass data into View:::make()
View::make('my-view')->with(['name' => 'Alex', 'date' => $now]); // pass data into View#with()
View::make('my-view')->withName('Alex')->withDate($now); // use fluent View#with()
So you'd just use them in the View::make() call as you presumably already are:
// in a route closure
Route::get('some-route', function () {
return View::make('menu', ['name' => 'Alex']);
});
// in a controller
public function someRoute()
{
return View::make('menu', ['name' => 'Alex']);
}
Interestingly, in a lot of frameworks/templating systems, if you wanted to include a partial, you'd pass the data you want to be available in that partial in the partial call, but Laravel doesn't quite do this. For example in a made-up system you may have something like this:
// in controller:
$this->render('home', ['name' => 'Alex', 'age' => 30]);
// home.php
<?php echo $name; ?>
<?php echo $this->partial('home-age', ['age' => $age]); ?>
// home-age.php
<?php echo $age; ?>
But in Laravel, all current view variables are automatically included into partials for you. Now I tend to like to specify the variables anyway (Blade does allow you to do this as above), and obviously it can be used to override a view variable:
// route:
return View::make('home', ['name' => 'Alex', 'age' => 30, 'gender' => 'male']);
// home.blade.php
{{ $name }}
#include('home-extra', ['age' => 20])
// home-extra.blade.php
{{ $age }}
{{ $gender }}
The above code would output:
Alex
20
male
So the age is overridden in the #include, but the un-overridden gender is just passed along. Hopefully that makes sense.

Resources