Thamaraiselvam
Thamaraiselvam

Reputation: 7080

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

<a href="{{ route('sub-project.view', request()->route()->parameters['id'] . '/' . $update->id) }}" class="btn btn-primary project-view">View</a>

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.

Upvotes: 0

Views: 2695

Answers (2)

Jesus Erwin Suarez
Jesus Erwin Suarez

Reputation: 1585

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.

Upvotes: 3

Raphael Cunha
Raphael Cunha

Reputation: 1114

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

Upvotes: 1

Related Questions