Laravel - Missing required parameters for Route - php

I have a problem with my routes in Laravel 7 not sure where I a, going wrong here.
I have this route declaration:
Route::group(['prefix'=>'config', 'namespace'=>'Config'], static function () {
Route::resource('id-generation', 'IDSettingsController', ['names'=>'config.id_generation'])->only(['index', 'edit', 'update']);
});
Then a controller:
public function edit(IdSetting $setting)
{
return view('config.id.edit')->with(['setting'=>$setting]);
}
Then a view:
<form method="post" action="{{route('config.id_generation.update', ['id_generation'=>$setting])}}">
#method('patch')
#csrf
<x-inputs.text-input name="prefix" :model-object="$setting" />
<x-inputs.button/>
</form>
But I keep getting errors:
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: config.id_generation.update] [URI: config/id-generation/{id_generation}].(View: F:\PROJECTS\PHP\app\resources\views\config\id\edit.blade.php)
In as much as I can see, I have done everything correctly. Even artisan command route:list can clearly show the route with its parameters etc.
Where might I be getting it wrong.
Surprisingly if i change it to {{url('config/id-generation/', $setting)}} everthing seeems to be working fine.

you have to match your model variable name to your ressource name, you can customize it with as:
Route::group(['prefix'=>'config', 'namespace'=>'Config'], static function () {
Route::resource('id-generation', 'IDSettingsController', ['names'=>'config.id_generation', 'as' => 'setting'])->only(['index', 'edit', 'update']);
});

Related

Missing required parameter when using injection

I have this route:
Route::resource('riskfields', RiskfieldsController::class)->except('show');
Within the RiskfieldController/edit I'm calling the edit view as:
public function edit(Riskfield $riskField)
{
return view('riskfields.edit', [
'riskField' => $riskField
]);
}
where Riskfield is the model.
So, inside edit.blade.php I have:
{{ Form::open(['route' => ['riskfields.update', $riskField->id], 'method' => 'put', 'autocomplete' => 'off']) }}
when I access to this endpoint: /admin/riskfields/1/edit
I get:
Missing required parameter for [Route: riskfields.update] [URI: admin/riskfields/{riskfield}] [Missing parameter: riskfield]. (View: /var/www/html/resources/views/riskfields/edit.blade.php)
The problem's that I have inject the model, so I don't know what's happening there.
This is the update method:
public function update(Riskfield $riskField, Request $request): RedirectResponse
Someone could help?
UPDATE:
dd ouput:
This problem stopped me for half an hour, but I hope my answer will help someone. Essentially I have to change this:
public function edit(Riskfield $riskField)
into:
public function edit(Riskfield $riskfield)
The parameter must have the same name convention defined within the route, so I've executed php artisan route:list and it was admin/riskfields/{riskfield}
Hope to help someone.

Laravel contact form is not working correctly

I have a problem with the Laravel site's form, when I send the form through the site it shows an error, using devTools it is noted that it is an error 500 of the Post method, but the form is sent anyway.
the following error appears in the laravel log file
[2021-12-15 11:27:49] production.ERROR: Missing required parameters for [Route: contato] [URI: {lang}/contato]. {"exception":"[object] (Illuminate\Routing\Exceptions\UrlGenerationException(code: 0): Missing required parameters for [Route: contato] [URI: {lang}/contato]. at /home/corstonecom/public_html/vendor/laravel/framework/src/Illuminate/Routing/Exceptions/UrlGenerationException.php:17)
In the form view it like this:
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', app()->getLocale()) }}" method="post">
In the routes file web.php it like this:
Route::view('/contato', 'fale-conosco')->name('contato');
Route::post('/contato', 'HomeController#enviarContato')->name('contato-enviar');
and in the controller it like this:
public function enviarContato(EnviaContatoRequest $request)
{
$inputs = $request->all();
$inputs['localidade'] = $inputs['cidade'] . '/' . $inputs['uf'];
$contato = Contatos::create($inputs);
Lead::fastSave([
'name' => $inputs['nome'],
'email' => $inputs['email'],
]);
Mail::send(new FaleConosco($contato));
Session::flash('contato_enviado', 'sucesso');
return redirect()->route('contato');
}
Where am i going wrong?
You would need to pass the lang parameter when calling the route method when doing the redirect in your Controller method as the route contato requires the lang parameter:
return redirect()->route('contato', ['lang' => app()->getLocale()]);
It is best to get in the habit of using an associative array for the parameters as any more than 1 parameter and they will need to be in an associative array for the URL generator to match them up.
If you don't want to have to constantly be passing this app()->getLocale() value to the URL helpers you can set a default for the lang parameter and it will be added for you. You could use a middleware to do this. Example of the functionality of setting a default value for a parameter:
public function handle($request, $next)
{
\URL::defaults([
'lang' => app()->getLocale(),
]);
return $next($request);
}
Now you don't have to pass the parameter for lang when generating URLs as it has a default value set.
If you already have a "locale" middleware that is handling the locale you can add this to it.
When you pass parameters to the route, you should pass it as an array with keys, so that Laravel knows what the parameter is. In this case, you would do
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', ['lang' => app()->getLocale()]) }}" method="post">
See https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes

SQLSTATE[22P02]: Invalid text representation error

Can't figure out why I am getting this error? I am using posgres I feel like I am missing some important link to figure this whole thing out ?
Basically I need to link the comment to the blog post with the id
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input
syntax for integer: "{blog}" (SQL: select * from "blogs" where "id" =
{blog} limit 1)
Comments controller
use App\Blog;
use App\Comment;
class CommentsController extends Controller
{
// add store method
public function store(Blog $blog)
{
Comment::create([
'body'=> request('body'),
'blog_id' => $blog->id
]);
return back();
}
}
web.php
Route::post('blog/{blog}/comments', 'CommentsController#store');
form head with request:
<form method="POST" action="/blog/{blog}/comments">
As you have it now, action="/blog/{blog}/comments"> will return just /blog/{blog}/comments as action, since {blog} is not parsed by Blade.
Give your route a name:
Route::post('blog/{blog}/comments', 'CommentsController#store')->name('blogcomments');
In your <form> set the action to the named route (blogcomments), giving it the current $blog as parameter:
<form method="POST" action="{{ route('blogcomments', ['blog' => $blog] }}">
Read more on named routes and parameters at https://laravel.com/docs/5.7/routing#named-routes
Not tested! ;)

Missing required parameter for Route

I am working with Laravel 5.3. I have a controller function that has $id has its argument
public function verifyMe ($id){
$user = User::findOrfail($id);
return view ('dashboard');
}
I have in my route, a url with this $id parameter.
Route::get('/verify/{id}', [
'uses' => 'UserController#verifyMe',
'as' => 'VerifyMe',
]);
Also in my blade template, I have this
<h3>To verify, Click Here. </h3>
But I get this error
Missing required parameters for [Route: verifyMe] [URI: verify/{id}].
I dont know what I am doing wrong.
In your template, remove $user->id and put auth()->user()->id and see whether it'll work.
The issue i think, is variable $user.
I had a similar problem, and tried a way like this
Try this
<h3>To verify, <a href="{{route(['verifyMe', 'id' => $user->id])}}">Click
Here.</a> </h3>
I hope it helps

Passing variable with route parameter when form is submitted Laravel 5.2

I have this in my form in the viewpage.php:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
And this in the route.php:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
And this is my TestController:
public function avaliarSubordinor(Request $request, $uid){
return $uid;
}
I get an error which says 'Missing required parameters for[Route: test.route] [URI: testing/{{id}}]. Essentially What i want is to pass a variable to my controller using a route with a parameter when form is submitted..
I dont know if I am doing this properlly..if anyone can help me or point me to an example so i can understand what I am doing wrong..
Laravel 5.2 Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile]
Using the above link I found a solution.. I changed:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
to
<form action="{{ route('test.route', [$params_id]) }}" method="GET" >
and this:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
to
Route::get('/testing/{id}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
and for reading value :
if ($request->id) {
}
And It works! But I wonder if anyone else can get a POST version working, or is GET the only way? I dont really know much about GET/POST request, only that it's used in forms and ajax.. Would really like to learn more about HTTP GET/POST, if anyone has anything to add please share!! thanks! Hope this answer will help someone!
For me this worked pretty well.
{{ Form::open(array('route' => array('user.show', $user->id))) }}
with class name
{{ Form::open(array('route' => array('user.show', $user->id), 'class' => 'section-top')) }}
Although it's an old post hopefully it will help others in future.
For me in Laravel 5.8 the POST method just worked fine.
HTML form:
<form method="POST" role="form" action="{{route('store_changed_role', [$user_id, $division_id])}}">
Route:
Route::post('/sotre_changed_role/{user_id}/{division_id}', 'Admin\UserController#store_changed_role')->name('store_changed_role');

Categories