In my Dashboard I would like to set a parameter ('state) in the MenuItem, this is how I did it :
public function configureMenuItems(): iterable
{
yield MenuItem::linkToCrud($this->translator->trans('admin.menu.quote_request'), 'fas fa-briefcase', QuoteRequest::class)->setQueryParameter('state', 'proceed');
}
When I go to the page, and I dump, I don't have neither in the dump and the url.
What is this method for ?
How to send a parameter ?
Related
I am trying to make my URL more SEO friendly on my Laravel application by replacing the ID number of a certain object by the name on the URL when going to that specific register show page. Anyone knows how?
This is what I got so far and it displays, as normal, the id as the last parameter of the URL:
web.php
Route::get('/job/show/{id}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
Controller method
public function show($id){
$job = Job::findOrFail($id);
return view('website.job')->with(compact('job'));
}
Blade page where there is the link to that page
{{$job->name}}
You can overwrite the key name of your Job model:
public function getRouteKeyName()
{
return 'name';
}
Then in your route simply use {job}:
Route::get('/job/show/{job}', ...);
And to call your route:
route('website.job.show', $job);
So your a tag would look like this:
{{ $job->name }}
Inside your controller, you can change the method's signature to receive the Job automatically:
public function show(Job $job)
{
return view('website.job')
->with(compact('job'));
}
For more information, look at customizing the key name under implicit binding: https://laravel.com/docs/5.8/routing#implicit-binding
You need simply to replace the id by the name :
Route::get('/job/show/{name}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
In the controller action:
public function show($name){
//Make sure to replace the 'name' string with the column name in your DB
$job = Job::where('name', $name)->first();
return view('website.job')->with(compact('job'));
}
Finally in the blade page :
{{$job->name}}
2 options:
1) one is like #zakaria-acharki wrote in his comment, by the name of the job and search by the name for fetching the data
2) the second is to do it like here in stackoverflow
to build the url with the id/name
in this way you will make sure to fetch and show the relevant job object by the unique ID
the route:
Route::get('/job/show/{id}/{name}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
in the controller, update the check if the name is equal to the job name (in case it was changed) to prevent duplicate pages url's
public function show($id, $name){
$job = Job::findOrFail($id);
// check here if( $job->name != $name ) {
// redirect 301 to url with the new name
// }
return view('website.job')->with(compact('job'));
}
in the blade.php :
{{$job->name}}
I have set up pagination and it's working correctly. But i am not able to redirect to the current page.
E.g: If i invoke update method on currencies?page=2. I will redirect to the currencies instead of currencies?page=2.
Controller - index function
public function index()
{
$currencies = Currency::paginate(5);
return view('admin.currencies.index')
->withCurrencies($currencies);
}
Controller - Edit function
public function edit($id)
{
$currency = Currency::findOrFail($id);
return view('admin.currencies.edit')
->withCurrency($currency);
}
Controller - Update function
public function update(Request $request, $id)
{
$currency = Currency::findOrFail($id);
$currency->name = $request->name;
$currency->rate = $request->rate;
$currency->save();
return redirect()->route('currencies.index')->with('message', 'Done');
}
Views
{{ $currencies->links() }}
Check out the documentation here https://laravel.com/docs/5.5/pagination#paginator-instance-methods
You can either keep track of the page number (from referral URL if update is on different page or from query param) and pass that along in your update function like this instead of redirecting to a route.
//$page = grab page number from the query param here.
return redirect('currencies?page='.$page);
or you can also modify your index controller where you pass the page number as optional param and if null default to page 1 and if present pass that in.
$results->url($page)
Good luck.
In case someone has to do redirection to current, or any page and works with named routes.
Tested: Laravel 5
Some assumptions out of the way.
route:
$this->get('/favourite/{columnSorted?}/{sortOrder?}', 'Favourites#index')->name('favourite.list');
assumed project url in browser:
columnSorted: 'title'
sortOrder: desc
http://yoursite.net/favourite/title/desc?page=3
Now, named route redirect to page 3.
As you can see in route above, columnSorted and sortOrder are dynamic (? after param in route, e.g.: {sortOrder?}).
What it means, is that route can have both, just one or none of them.
If you wish to pass them to param array in route, you can do something like this:
/*prep redirect to, where user was params*/
$routeParams = [];
$q = '?';
if ($request->columnSorted)
{
$routeParams['columnSorted'] = $request->columnSorted;
}
if ($request->sortOrder)
{
$routeParams['sortOrder'] = $request->sortOrder;
$q = '';
}
if ($request->page)
{
$routeParams[] = $q . 'page=' . $request->page;
}
return redirect()->route('favourite.list', $routeParams);
Note above that '$q' parameter.
Last (and only last) route parameter $q must not pass '?', or constructed route from named route will have double '??' looking like:
http://yoursite.net/favourite/title/desc??page=3
... and redirect will fail.
Page number you can get from request:
$request->get('page');
// or
$request->page
... and pass it to method that will do redirect.
You must add a query string parameter to the route.
return redirect()
->route('currencies.index', ['page' => $request->get('page', 1)])
->with('message', 'Done');
I have the following route :
Route::get('/web/{cat_id?}/{post_type}','WebPostsController#index')
->where('post_type', '(pictures|videos|links|companies)')
->name('post_index');
Controller file :
public function index($post_type,$post_id = null,$cat_id = null)
{
dd($post_type);
}
When access the URL /public/web/15/videos
It return '15' which is supposed to return {post_type} value (videos) while it returning the {cat_id?} value.
The variables passed in to the action have to follow the definition in the route.
That is, in your case, you expect the cat_id first, then the post_type which means that you will have to define the method as:
public function index($cat_id, $post_type) { }
Furthermore, I expect that you will have some issues with it still, cause you can not have an optional parameter and then a forced parameter. So either move the $cat_id param as a optional to the end of the route and use $post_type before (/public/web/videos/15) or make both forced.
I want to pass multiple ID's to my controller.First for Categories and second for Food items as show in function show
My Controller is:
public function index()
{
$Foods=Food::all();
$Category=Categories::all();
return view('index.welcome', compact('Foods','Category'));
}
public function show($Food_id,$Category_id)
{
$food = Food::with('restaurant','categories')->findOrFail($Food_id);
$category = Categories::with('food')->findOrFail($Category_id);
return view('index.show', compact('food','category'));
}
My Routes are:
Route::get('index','DetailsController#index');
Route::get('index/{Food_id?}', 'DetailsController#show');
But it returns me error "Missing argument 2 for App\Http\Controllers\Detailscontroller::show()".Where is the problem in this?
You are calling a method which has 2 arguments, but in the route file you're only specifying one parameter.
Maybe your route should look something like this:
Route::get('index/{Food_id?}/{Category_id}', 'DetailsController#show');
Then you would be passing both variables.
Or modify your method declaration and remove the $Category_id parameter, as you are not passing it to the method, like this:
public function show($Food_id)
I struggle to understand how I can add a second parameter ($id) to pass along with this callback in my php class:
($this, 'updateDuplicateAcronym')
The method updateDuplicateAcronym should recieve 2 parameters, one by reference from the form where this code belongs (which works fine), and one more that I need in order to do some checks.
Someone who knows how to do this?
Here is my method to pass parameters :
$callBackFunction = function($p1, $p2) {
...
}
baseFunction($context, $callable, $params)
{
//Main process
call_user_func($callable, $params);
}
// Running
baseFunction($this, $callBackFunction, array(1, 42));