how to pass multiple values to routes from controller in laravel - php

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]) }}.

Related

I want to pass two parameters to the URL

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])

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
}

How to build path to route in Laravel with parameter?

I have this route
Route::get('org/edit/{id}/', ['as' => 'org.edit', 'uses' => 'OrgController#edit']);
And then create a link to this route by using Laravel Blade template:
<span class="glyphicon glyphicon-edit"></span>
What I expect to see:
/org/edit/123/
What I get:
/org/edit?123
What am I doing wrong?
Try passing it as a key value pair.
{{ route('org.edit', ['id' => $org->id]) }}
Dmitriy,
in this case you can try two ways.
{{ route('org.edit', ['id' => $org->id,]) }}
{{ route('org.edit', $org->id) }}
Check this out. :)

Laravel Passing Data to Controller

I just starting learning laravel and was wondering how to pass data unrelated to the route to a controller. What I'm trying to accomplish is create a todo item that is able to have nested items.
View
<a class="btn btn-success" href="{{route('lists.items.create',4)}}">Create New Item</a>
The 4 is just a hard-coded example to see if it was working.
Controller
public function create(TodoList $list, $item_id = null)
{
dd($item_id);
return view('items.create', compact('list'));
}
So if your creating an item and don't pass in a parameter for id, it will default to null otherwise set it to whatever was passed in. However I'm getting a NotFoundHttpException. How would I be able to accomplish this.
Any help Welcome :)
You need to define the route, for example:
Route::get('create-item/{id}', [
'as' => 'lists.items.create',
'uses' => 'MyController#create'
])
Now, call the route like:
<a class="btn btn-success" href="{{route('lists.items.create', ['id' => 4])}}">Create New Item</a>

Laravel form action

I'm having a hard time setting up simple links/actions.
In my index view, I have this little form that I want to launch the getTest action in the ProjectsController when I click on the button:
{{ Form::open(array('action' => array('ProjectsController#getTest', $project->id))) }}
<button type="submit"><i class="icon-arrow-up"></i></button>
{{ Form::close() }}
This is the getTest function :
public function getTest(){
echo "test";
return 'test';
}
But this keeps getting me a "Array_combine(): Both parameters should have an equal number of elements" error.
I tried making this work with a route. with this form open instead :
{{ Form::open(['method' => 'GET', 'route' => ['test_route', $project->id]]) }}
And this route :
Route::get('projects/test', array('as' => 'test_route', 'uses' =>'ProjectsController#getTest'));
But I still have the same error.
I can't find any good doc on routing/sending to actions that don't give me this problem. I don't see what
Your route doesn't need parameter, so I think this code is sufficient:
{{ Form::open(['method' => 'GET', 'route' => 'test_route']) }}
I believe the problem is you are adding parameters to the action, but you are not managing those parameters in your routes, nor is your getTest() function accepting any parameters. Another problem is you are setting your route as a GET route, but your form is going to be using POST.
It would be much easier instead on your form to use Form::hidden('id', $project->id); And then in your getTest() function, you could get the variable using $id = Input::get('id');. You'd also be able to use your route name in your form as well. Form::open(array('route'=> 'test_route', method=> 'get'));

Categories