Reputation: 1471
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?
Upvotes: 17
Views: 149600
Reputation: 102
I used the following code and it worked.
<button class="btn border button" id="go-back-btn" onclick="window.location='{!! route('getDashboardScreen') !!}?MilkCompanyID={!!session('MilkCompanyID')!!}'">Go back</button>
Upvotes: 0
Reputation: 3274
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('showProblemEditPage');
Route::post('/problems/{problem-id}/edit', 'AdminController@updateProblem')->name('updateProblem');
Now you use this route in blade with just name for post and get both
Exmaple of GET route :
<button type="button" onclick="window.location='{{ route("showProblemEditPage",[$problemIdParameter]) }}'">Button</button>
OR
<a href="{{ route("showProblemEditPage",[$problemIdParameter]) }}">Button</button>
OR
<button type="button redirectToUrl" data-redirect-url="{{ route("showProblemEditPage",[$problemIdParameter]) }}">Button</button>
<script>
$(document).on('click','.redirectToUrl',function(){
let getRedirectUrl = $(this).attr('data-redirect-url');
window.location.href= getRedirectUrl;
});
</script>
Example of POST route :
In case if you are submiting form via submit button click
<form action={{ route('updateProblem',[$problemIdParameter]) }}>
.....
<button type="submit">Submit</button>
</form>
In case if you are submiting form via ajax on button click
<form action={{ route('updateProblem',[$problemIdParameter]) }}>
.....
<button type="button" class="submitForm" >Submit</button>
</form>
<script>
$(document).on('click','.submitForm',function(){
let getForm = $(this).parents('form');
let getFormActionUrl = getForm.attr('action');
$.ajax({
url: getFormActionUrl,
.....
.....
.....
.....
.....
});
});
</script>
Upvotes: 9
Reputation: 4141
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');
Upvotes: 1
Reputation: 3486
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>
Upvotes: 7
Reputation: 2190
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.
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
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'));
}
Upvotes: 26