I currently have a route that looks like this:
Route::get('/profile/{username}',array(
'as' => 'profile',
'uses' => 'ProfileController#getProfile'
));
The idea is that a link, like http://www.website-here.com/profile/johnnyappleseed, containing a username will do a search for the username in the database and return the profile that matches based on the username in the URL. How would I create a link to the logged in User's profile using the same route. Currently I have
<li>Profile</li>
But that seems incorrect and that there should be a way to do it using some of Laravels helpers.
You can use the route helper and pass the route name and an array of parameters needed:
<li>
<a href="{{ route('profile', [Auth::user()->username]) }}">
Profile
</a>
</li>
If you want to go all the way and use even more Laravel helpers, you can use the HTML facade and generate the entire link tag in one line:
<li>
{{ HTML::link(route('profile', [Auth::user()->username]), 'Profile') }}
<li>
Related
Route::get('/products/{page?}', 'ProductController#index')->name('products');
Route::get('/products/{category}/{page?}', 'ProductController#indexCategory')->name('category.products');
I have two route like this.
When i call the second route like :
<a href="{!! route('category.products', ['category' => $category['translation']['en']['slug']]) !!}" class="list-group-item active">
it calls #index method instead of #indexCategory.
idk why?
I want to make
GO TO INDEX METHOD
/products, /products/2, products/3
GO TO INDEXCATEGORY METHOD
/products/new-collection, /products/new-collection/2,
/products/new-collection/3
I think you need to update the route like this.
Route::get('/products/{page?}', 'ProductController#index')->name('products');
Route::get('/products-category/{category}/{page?}', 'ProductController#indexCategory')->name('category.products');
Because naming convention conflicts your route. so make sure route URL is unique.
And then call your route like:
<a href="{!! route('category.products', ['category' => $category['translation']['en']['slug']]) !!}" class="list-group-item active">
OR
If you would like to do not change the route URL then make sure your second perameter required. not optional.
While creating an admin menu, i was wondering how to set active class item. The menu item have to stay active if the controller is showing, editing or doing something else.
sidebar.blade.php
<li class="nav-item">
<a class="nav-link {{ (Route::current()->getName() == 'posts' ? 'active' : '') }}" href="/admin/posts">Posts</a>
</li>
routes/web.php
// POSTS
Route::resource('/admin/posts', 'Admin\PostController');
How to set a shared name for all resources (index, create, show, etc.)?
I was hoping to do something like this but...
Route::resource('/admin/posts', 'Admin\PostController')->name('posts');
Thanks
Naming Resource Routes
By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your options:
Route::resource('photos', 'PhotoController')->names([
'create' => 'photos.build'
]);
You can find more options in the documentation.
work in laravel 9 try in other version
Route::resource('vente', VenteController::class,
['names'=>['index'=>'vente.index']]);
call it in menu
vente
I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])
how to remove question mark form url in laravel
i use laravel 5.4 and i get this trey to use query in the link but it don't work
http://localhost/shooping-cart/public/add-to-cart/id?1
i want to remove id and question mark form url
i use laravel 5.4
Route::get('/add-to-cart/{id}', [
'uses' => 'ProductController#getAddToCart',
'as' => 'product.addToCart'
]);
Looks like the problem is in the way you generate the link. For this route you should use route() helper like this:
{{ route('product.addToCart', $productId) }}
Or create a link manually:
{{ url('add-to-cart/'.$productId) }}
Use ? mark after id like {id?}
And in the view file, generate link like this..
Link Name
It will definitely work.
Route::get('/add-to-cart/{id?}', [
'uses' => 'ProductController#getAddToCart',
'as' => 'product.addToCart'
]);
{{ route('product.addToCart', $productId) }}
I think this is just the way Laravel constructs the URL. Its more of an aesthetic than functional choice. If you do not wish to see a question mark in your url when performing GET request then use a named route with parameters.
View
If you access your controller by matching URL thru the router class.
<form method="get" action="/questions/{{$task->id}}">
<button type="submit" class="btn btn-info btn-sm">View</button>
</form>
You will see a question mark "?" appended to the end of your URL.
I use davejamesmiller Breadcrumbs package. I am wondering how to pass a parameter to a breadcrumb, something like an id.
In the docs (here) it says that is possible, but can't find the way to do it.
My goal es to do a breadcrumb like this: Dashboard \ User \ New Model. Where New Model its a form to add model data with some relationship with the user. Without the user_id param the link for User won't work.
Any idea?
You can pass global variable
\View::share ( 'variable2', $variable2 );
if render breadcrumbs in layout
or You need render breadcrumbs in `user.new_model.blade
#section('content')
{!! Breadcrumbs::render('page', $page) !!}
#stop`
my way
Create template
breadcrumbs.blade.php
with content
#if(!empty($breadcrumbs))
<ol class="breadcrumb">
<li>{!! link_to_route('main', 'Home') !!}</li>
#foreach($breadcrumbs as $bread)
#if(isset($bread['url']))
<li>{!! link_to($bread['url'], $bread['name']) !!}</li>
#else
<li>{!! $bread['name'] !!}</li>
#endif
#endforeach
</ol>
#endif
and connect it to layout
#include('breadcrumbs')
and in your action pass array of links
\View::share('breadcrumbs', [
['url' => route('collection.show', ['id'=>$data->collection, 'url'=>$data->collection]), 'name' => $data->collection->name],
['name' => $data->article]
]);
There is another way. As general, in each view just calling Breadcrumbs::render() should create the hierarchy of the breadcrumbs links depending on the routes defined in routes/breadcrumbs.php.
There are two essential points that you have keep in mind to go further with this solution:
The callback function that found in breadcrumbs.php routs definition is the place from which you should pass your parameters.
Giving correct name to your web route from routes/web.php which will be used later in routes/breadcrumbs.php
Checkout the following code snippets that demonstrates the above two points:
//Point1: routes/breadcrumbs.php
Breadcrumbs::register('job.edit', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job','job');
$breadcrumbs->push($title, route('job.edit', $job));
});
Breadcrumbs::register('job.edit.install', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job.edit',$job, $title);
$breadcrumbs->push('Job Install Equipments', route('job.edit.install','job'));
});
In the above code we passed $job and $title through the callback function.
//Point2 routes/web.php
Route::get('/job/edit/{job}', 'JobController#edit')->name('job.edit');
Route::get('/job/install-equipments/{job}', 'JobController#installEquipments')->name('job.edit.install');
We give a name to the route through the name method , Laravel 5.4, which allow us to define the routes correctly in Point1.
The last step, is what you have do in the view file. Here I will show you the last one regarding /job/install-equipments which should be rendered in the breadcrumb as the last element and its parent is job/edit with parameter job which handles the primary key id
//install.equipments.blade.php
#extends('layouts.main')
#section('content')
{!! Breadcrumbs::render('job.edit.install',$job->id, __('Edit').': '.$job->title) !!}
The above will render a breacrumbs look like:
Home / Job / Edit: title of
job / Job Install Equipment
The required parameters that handles breadcrumbs render are supplied un the above render method i.e through $job->id and __('Edit').': '.$job->title) , the last just adjusting the text and it could be done inside the callback function of breadcrumbs routes.