Laravel Missing required parameters - php

I tried to create a new route with a controller that uses an id parameter, and a link to this.
The error is when I try to create a link to a controller.
The error that I got is:
"Missing required parameters for [Route: notas.detalle] [URI: detalle/{id}]. (View: /var/www/html/laravel/blog/resources/views/producto.blade.php)"
The route:
Route::get('detalle/{id}', 'productoController#detalle')>name('notas.detalle');
The blade template:
#foreach($notas as $nota)
{{$nota->id}}
{{$nota->created_at}}
{{$nota->updated_at}}
#endforeach
What can be the problem?

Laravel route helpers can take as a second parameter, an associative array with all the keys (require parameter) and it values.
so is your case it would be something like this
route('notas.detalle', ['id' => 1])
but it could be multiple parameter so is your route had something like 'notas/{id}/student/{student}
then you could do the following.
route('notas.detalle', ['id' => 1, student => 129483])
here is a link to the docs where you can see this in more details.
https://laravel.com/docs/5.8/routing#named-routes

The problem is that the variable must be after comma.
I think this will work:
{{$nota->id}}

Related

Passing parameter using routes laravel 5.8

I have two routes
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
but when i access the second routes, it always gets the first routes why?
and how to fix it?
thanks for helping me
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->where(['fasilitas_id' => '[0-9]+', 'any' => '[0-9]+']);
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->where('kf_id' => '[0-9]+');
For more info Regular Expression Constraints
Another way to pass it define name route.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('fasilitas.example1');
<a href="{{ route('fasilitas.example1',['fasilitas_id'=>1,'any'=>2]) }}">
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('fasilitas.example2');
<a href="{{ route('fasilitas.example2',['kf_id'=>1]) }}">
When hitting /fasilitas/get_kategori you trigger the first route, with get_kategori being the {fasilitas_id}.
Change the order of your routes, so /fasilitas/get_kategori gets triggered first:
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
The second segment of the first route is a wildcard, that means it could be anything.
When the second route is called in browser the second segment (/get_kategori/) is passing through the wildcard of the first route.
Changing the route order may solve the problem. But the best practice is changing the route name. Example:
Route::get('/fasilitas/something_else/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Try with name . It will be more efficient
Fasilitas !
May be you tried to write facilities.
Whatever, am writing base on ur method.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('detail.fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('get.kategori');
How to call at front page for two parameter and single parater ?
Have a look below .
For detail.fasilitas
Detail Fasilitas
For Single Parameter : get.kategori
Get Kategori
Try it.
Let me know its work or not .
Have fun with code.

Laravel named route URL with parameter not working as expected

I'm trying to generate a url with parameter using laravel route helper,
route('frontend.admin.categories.edit', $catRequest->id)
but this is the generated url
http://localhost:8000/admin/categories/edit?1
I need a URL like this
http://localhost:8000/admin/categories/edit/1
And this is my route
Route::get('admin/categories/edit/{id}', 'CategoryController#edit')
->name('admin.categories.edit');
What is the issue here?
You can specify the parameter(s) you are replacing by passing an associative array instead of a variable.
Change,
route('frontend.admin.categories.edit', $catRequest->id)
To
route('frontend.admin.categories.edit', [
'id' => $catRequest->id
]);
Edit #1
You are calling the wrong route, you named your route as admin.categories.edit in the definition yet you are calling frontend.admin.categories.edit within the helper function which is not the originally defined route. So your code should be:
route('admin.categories.edit', [
'id' => $catRequest->id
]);
Reading Material
Named Routes
You need to use an array for your params :
route('frontend.admin.categories.edit', ['id' => $catRequest->id])
If not, all params will be used as $_GET params.
Try to view your route list using php artisan route:list. If you found your route in list with correct Uri then see that you are not again specify that route with same Uri or same alias name in your route file.
Hope this helps :)
You may have another route with the same name right after this one in the code that may be overwriting the above route params, i had the same problem and in my case this is what was happening, i had two routes definitions using the same name but with different http methods like the example below.
Ex:
Route::get('/route1/{param1}');
Route::post('/route1');
redirect()->route('/route1', ['test']); // output: /route1?test
When i've changed it to:
Route::post('/route1');
Route::get('/route1/{param1}');
redirect()->route('/route1', ['test']); // output: /route1/test
then i got the desired result.

Alias for a route with a fixed parameter value

I have this route:
Route::get('/MyModel/{id}', 'MyController#show');
The method show() accepts a parameter called id and I want to setup an alias for /MyModel/1 so it's accesible from /MyCustomURL.
I already tried a few combinations, like:
Route::get('/MyCustomURL', ['uses' => 'MyController#show', 'id' => 1]);
But I keep getting missing required argument error for method show().
Is there a clean way to achieve this in Laravel?
In Laravel 5.4 (or maybe earlier) you can use defaults function in your routes file.
Here is example:
Route::get('/alias', 'MyController#show')->defaults('id', 1);
In this case you don't need to add additional method in your controller.
In same controller (in your case MyController ?) you should create one new method:
public function showAliased()
{
return $this->show(1);
}
and now you can define your aliased route like so:
Route::get('/MyCustomURL', 'MyController#showAliased');
define your route like this:
you can use "as" to give your route any name that you need.
Route::get('/MyModel/{id}' , [
'as'=>'Camilo.model.show',
'uses' => 'MyController#show' ,
]) ;
now if you want to access this route, you can generate url for it, based on its name like this:
route('Camilo.model.show', ['id' =>1]) ;
Route::get('MyModel/{id}', 'MyController#show');
not
Route::get('/MyModel/{id}', 'MyController#show');
Good Luck!

Laravel 5 route not resolving

I cannot get the named routes to resolve in my controllers. I am very new to Laravel so I am sure I missed something obvious. Any help is appreciated.
I have the following code in routes.php:
Route::get('password/email', ['uses' => 'Auth\PasswordController#getEmail', 'as', 'session.getEmail']);
Then in one of the Controllers I want to extract the route and place it in a variable I am passing to a blade template like this:
$forgot_password_link = route('session.getEmail');
return view('auth.login', compact('logo', 'forgot_password_link'));
This is then used in the form like this:
Forgot Your Password?
But I get the following error when I load the page:
InvalidArgumentException in UrlGenerator.php line 278:
Route [session.getEmail] not defined.
'as', 'session.getEmail'
this should be key=>value pair

How to put a parameter in a Laravel controller method not obligatory?

I would like to know how to put a parameter in a Laravel controller method not obligatory, as I've got a route like this (I'm using the ? to say it's not obligatory):
Route::get('/account/student/create/{student_id?}', array(
'as' => 'account-student-create',
'uses' => 'StudentController#getCreate'
));
I've got in my controller:
getCreate($student_id){...}
Put if don't fill a parameter in the route I've got a warning message:
Missing argument 1 for StudentController::getCreate()
Thanks for helping!
Jean
Add a default value to your controller method declaration:
getCreate($student_id=null){...}
That way when the method is called without a value, $student_id will be set to null.
you could write your function with a default value
public function getCreate($student_id = 'defaultValue')
{
...
}

Categories