I want to pass two parameters to the URL - php

I want to pass two parameters with the route helper
One is the thread ID and the other is less so I want to pass both
index.blade.php
#foreach($works as $work)
<tr>
<td>{{$work->input_person}}</td>
web.php
Route::get('/work/edit/{id}/{project}', 'WorkController#edit')->name('work.edit');
Incidentally, the error appears like this
Missing required parameters for [Route: work.edit] [URI: work/edit/{id}/{project}].
I don't have a good idea of ​​what to do

Pass it in the same array.
<a href="{{ route('work.edit', ['id' => $work->id,'project' => $param->id])}}">

Try this:
route('work.edit', ['id' => $work->id, 'project' => $param->id])

Related

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

Laravel 5 - How to pass multiple parameter in GET route?

Can anyone please help me to pass multiple parameters in GET method, I have following codes -
in blade -
#if(!isset($model->id_car_type))
<a class="btn btn-search-red" href="{{ route('frontend.model', [Request::segment(2),$model->year,$model->niceName]) }}">Select</a>
#else
<a class="btn btn-search-red" href="{{ route('frontend.model', array(Request::segment(2),$model->year,$model->niceName, $model->id_car_type)) }}">Select</a>
#endif
In route -
Route::get('/model/{make}/{year}/{niceName}/{type?}', 'GeneralController#trimShowByNiceName')->name('frontend.model');
But it is throwing error -
Missing required parameters for [Route: frontend.model] [URI:
model/make/{make}/year/{year}/niceName/{niceName}/{type?}]
To pass parameters in a route use an array with the paramter names as keys:
{{ route('frontend.model', ['make' => 'Ford', 'year' => 1988, 'niceName' => 'Ford Escort']) }}
https://laravel.com/docs/5.7/routing#named-routes
In Laravel 5.2 use the following example:
In the view.blade.php
Page
In the route.php
`Route::get('users/{id}{name}', 'UsersController#searchUsers'); // this gets the id, name` from the view url.
In the controller method, pass the parameters as function parameters as follows:
public function searchUsers($id, $name)
{
// your code here that use the parameters
}

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

how to pass multiple values to routes from controller in laravel

route
Route::get('/dashboard/view-sub-project/{pid}/{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
View
View
Values of var
request()->route()->parameters['id'] is 2
$update->id is 1
I have defined router correctly on web.php and view but still, it throws an error
Missing required parameters for [Route: sub-project.view] [URI:
dashboard/view-sub-project/{pid}/{sid}]. (View:
/var/www/html/groot-server/resources/views/project/view.blade.php)
I have tried to change my router like this also
Route::get('/dashboard/view-sub-project/{pid}{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
Still got the same error.
Try adding parameters in array.
Route::get('/dashboard/view-sub-project/{pid}/{sid}','SubProjectController#view')
->name('sub-project.view')
->middleware('auth');
<a href="{{ route('sub-project.view',
[
'pid' => request()->route()->parameters['id'],
'sid' => $update->id
]
) }}" class="btn btn-primary project-view">
View
</a>
Hope this helps.
On your view, since you're using the route function to build the url you can do the following.
<a href="{{ route('sub-project.view', [
'pid' => request()->route()->parameters['id'],
'sid' => '$update->id'
]) }}" class="btn btn-primary project-view">View</a>
You can also view it in the Laravel Helper Function.
If you only have one parameter in the route you can just pass the value. Let's say you had a route that only took a post ID, Route::get('/posts/{post}/edit')->name(edit). On your view you can then do {{ route('edit', $post->id) }}.
When you have multiple values being passed to the route url as you have in your case you pass an array of item with the key being the same as the route parameter.
Let's say you have another route Route::get('/posts/{post}/comments/{comment}')->name(post.comment). On your view you can do {{ route('post.comment', ['post' => $post->id, 'commment' => $comment->id]) }}.

Laravel 5 - link_to_route() method making my route parameters to change into Query String by adding "?" at the end

After hours of searching I still could not find my answer regarding L5.
What my issue is :
I want to make a link something like this:
localhost:800/songs/you-drive-me-crazy
BUT what is get is:
localhost:800/songs?you-drive-me-crazy
my route parameter is changing into query string.
//routes.php
$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});
$router->get('songs', ['as' => 'songs.index', 'uses' => 'SongsController#index'] );
$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController#show'] );
I am using:
{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}
I have tried everything but not succeeded yet,your suggestion may be helpful.
Thanks.
Your usage of link_to_route is incorrect:
{!! link_to_route('songs.index', [$song->title, $song->slug]) !!}
The first parameter is the route name, the second parameter is an array of route parameters, preferably using key value. Because you did not show your defined route, it's hard to guess what this associative array should look like:
{!! link_to_route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
Also I advise you to use the documented functions: route(), see: http://laravel.com/docs/5.0/helpers#urls
A correctly requested route using route():
{!! route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
A properly formatted route would then be:
Route::get('songs/{title}/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/slug
If you only want to add the title to the URL but not the slug, use a route like this:
Route::get('songs/{title}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/?slug=slug
Using
Route::get('songs/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
The URL will be like: http://localhost:800/songs/you-drive-me-crazy/?title=title assuming the slug now is you-drive-me-crazy
Any added parameter in a route() call will be added as a GET parameter if it's not existing in the route definition.
fixed it, thanks for your great concerns and suggestions.
I was linking to wrong route here:
`{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}`
now, I changed it as :
`{!! link_to_route('songs.show', $song->title, [$song->slug]) !!}`
and it did the trick.

Categories