11
votes

I am using the Laravel framework and the blade templating engine for one of my projects, where I have a route which looks like

Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem');

I have editProblem method in AdminController which returns a view

public function editProblem(Problem $problem) {
        return view('admin.problem-edit', compact('problem'));
    }

and I have a button on a view which looks like

<button class="btn btn-xs btn-info pull-right">Edit</button>

Now I want to call this route with the $problem->id when the button will be clicked. I need to pass these value on the route.

how can I do that?

4

4 Answers

20
votes

In my opnion, you should use url() Laravel method

To call you route with the problem's id you can do:

<a href="{{ url('/problems/' . $problem->id . '/edit') }}" class="btn btn-xs btn-info pull-right">Edit</a>

I used an anchor tag, but it will be rendered like you button tag because I kept the same style class you have defined.

Why you should use url() method ?

The reason is simple, the url method will get the full url to your controller. If you don't use this, href link will get appended with current url.

For example, supose you button is located inside a given page

yourdomain.com/a-given-page/

when someone click in your button, the result will be:

yourdomain.com/a-given-page/problems/{problem-id}/edit

when you would like to get this:

yourdomain.com/problems/{problem-id}/edit

Some considerations about your editProblem method

Your route has the '$id', so you need to receive this '$id' in your method

public function editProblem($problem_id) {

$problem = \App\Problem::find($problem_id); //If you have your model 'Problem' located in your App folder

return view('admin.problem-edit', compact('problem'));
}
6
votes

Try This:

<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>

Little Suggestion: When you're defining routes in laravel give it a unique name, it helps you to keep track on each url like this

Route::get('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');
Route::post('/problems/{problem-id}/edit', 'AdminController@editProblem')->name('pEdit');

Now you use this route in blade with just name for post and get both

<button type="button" onclick="window.location='{{ route("pEdit",array("parameter1")) }}'">Button</button>
5
votes

You will need to create a link to this route:

<a href="/problems/{{ $problem->id }}/edit" class="btn btn-xs btn-info pull-right">Edit</a>

If you use named routes, this will be even easier:

Route::get('/problems/{problem-id}/edit', ['as' => 'problems.edit', 'uses' => 'AdminController@editProblem']);

And then you just need to call the route method:

<a href="{{ route('problems.edit', $problem->id) }}" class="btn btn-xs btn-info pull-right">Edit</a>
0
votes

Was asked for route inquiry. so that :

<button  onclick="window.location='{{ route("some_route_name") }}'"...>
</button>

in web.php

Route::get('/some_route_url', [SomeRouteController::class,'some_func'])->name('some_route_name');