Dropdown with 'Delete' Request in Laravel? [duplicate] - php

This question already has answers here:
laravel method in href link?
(4 answers)
Closed 6 years ago.
I allready asked something like that, but haven't got something that helped me out. So I'm sorry for asking again..
First I've created a Form with a submit button that directs me to my delete route. The Form had a 'DELETE' as a method.
{!! Former::danger_submit('Delete') !!}
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController#destroythread", $comment->id)) !!}
{!! Former::close() !!}
This worked perfect. That means my controller function, the routes and the form haven't any mistakes.
Now I decided, that I want a dropdown. In the dropdown, there should be written: delete and edit. Lets focus on the delete.
The dropdown links looking like this:
<ul class="dropdown-menu">
<li>Edit</li>
<li>Delete</li></ul> // <- Delete
I can't put my Laravel form inside this <li> tag of course.. Thats why I tried to do a link that directs me to my laravel delete route.
Like this:
<li>Delete</li>
but this haven't worked because this gives me a 'GET' request, not a 'DELETE'.
Well, now I'm stuck. I'm really sorry for asking this again..
I hope someone can help me out.
My laravel Delete Route is this:
Route::delete('/show/{id}', ['as' => 'destroythread', 'uses' => 'Test\\TestController#destroythread']);
and my Controller function is this:
public function destroythread($id) {
$comment = Comment::query()->findOrFail($id);
$comment->delete();
return redirect()->back();
}
My last question about this problem couldn't help me out. A user said I can use ajax but I don't really understand how that works and don't really liked this way at all.
Thanks for any help!
Edit by #Joost answer:
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController#destroythread", $comment->id)) !!}
<li><button>Delete</button></li>
{!! Former::close() !!}
worked but now there is a real button in my dropdown.. this don't look good. Is there any way that there just will stand a: Delete ?

You're saying that your form works. So you could just use a link instead of a button and simple JS like:
<a href="#" onclick="document.yourFormName.submit(); return false;">
Or you could try:
<a href="#" onclick="parentNode.submit(); return false;">

Related

Best way to keep pagination after editing a record

I'm very new to Laravel and I'm trying to make a crud application with a view for each db table that has a list with edit buttons on each record.
So far I have the paginated list which is working good and an edit view that is working as well.
The problem I'm facing is that whatever page I'm in I can click the edit button and edit the record successfully, but when I save the record I always go back to page 1.
I don't know and I'd like to know which is the correct (best) way to achive this.
Should I use session? Or maybe append the page parameter to the edit form action? Or else?
EDIT:
this is what edit method returns:
return redirect('/lyrics')->with('success', 'Lyric updated!');
Thank you for any help that points me in the right direction.
You can pass the page parameter to the edit page and then after the user update the record, you can redirect to the page you were in.
Something like this:
In the link where you call the edit page:
Edit
In your edit form:
<form>
... fields
<input type="hidden" name='page' value={{ app('request')->input('page') }}
</form>
Then in your update method at the controller
public function update($request)
{
// update
return redirect('homepage', ['page' => $request->page]);
}

Laravel 5 Can't send data from url

Hi Im currently trying to make a basic crud for my questions resource on laravel 5
so far so good, but now Im having troubles displaying the edit view, because the url is not being created correctly when I try to send the resource id in the url
here's the anchor Im using
<button class="submit-form button button btn btn-primary" style="margin: 0 1em;" type="submit">Editar</button>
here's the route in my routes file
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin/questions/update');
the method in the controller works just fine, when I manually type this url
/admin/preguntas/editar/4
It shows the view without problems, but when I go from the anchor the url it goes is this one
/admin/preguntas/editar?4
of course the 4 is the id from my resource, but why is not typing the correct url?
thanks in advance
You can't wrap a hyperlink around a button so my assumption is your problem is related to the form action (since button type is submit), not the a href.
Since it appears you're using bootstrap, there is no need to use a button to get the styling of a button.
<a class="btn btn-primary"> will work just fine.
And your "QuestionsController#edit" accepts "id" argument?
like
function edit($id){}
And I think, your are using wrong route link helper
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
First write proper link button as Devon said:
Editar
Second you must write proper route name format:
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin.questions.update');
Hope it will help you to solve the problem :)

Laravel form method get - url parameters

I got a form in laravel with Method='GET'
{{ Form::open(array('method' => 'get')) }}
I got a few input fields in there: city, skillLevel, province and category.
When i submit the form i get a awfull url like this:
http://localhost/vacatures?city=somehwhere&skillLevel=junior&province=Zeeland&category=django
but i want something like this:
http://localhost/vacatures/somewhere/junior/Zeeland/django
How do i achieve this? I've tried giving at a route attribute and a action actribute, but that didnt work out.
Maybe this topic will help you - How To Pass GET Parameters To Laravel From With GET Method ?
There are 2 nice answers, which should suit to your problem.

Issue with Laravel 4 Routes?

I'm trying to learn laravel, but I'm running into an issue with routing.
I have the following in my Routes file:
Route::get('home', function()
{
return View::make('home');
});
Which works if I type
http://localhost/laravel/public/home
However, on a another page I have a form, that when submitted should take me to that page like so:
{{ Form::open(array('url' => 'home')) }}
Now this takes me to the correct address, but throws an exception. But, if I reload the page with the same URL then the page loads correctly. So what is the issue here? Is there a problem with the way my form is set up?
Route::get is when you want to issue a get request to the page, to get the contents basically. If you want to post to a page, you need to do, in addition to your Route::get,
Route::post
Another option that people will tell you is:
Route::any
but I would advise staying away from this because the logic will probably be different for the two routes.

Laravel back button

I am trying to create a simple back button on a page. The user can arrive to this page from two different pages so I would like to know from which page he arrived. Is that possible?
In Laravel, you can do something like this: Back (assuming you're using Blade).
Laravel 4
{{ URL::previous() }}
Laravel 5+
{{ url()->previous() }}
Laravel documentation
I know this is an oldish question but I found it whilst looking for the same solution. The solution above doesn't appear to work in Laravel 4, you can however use this now:
Go Back
Hope this helps people who look for this feature in L4
(Source: https://github.com/laravel/framework/pull/501/commits)
Laravel 5.2+, back button
Back
Indeed using {{ URL:previous() }} do work, but if you're using a same named route to display multiple views, it will take you back to the first endpoint of this route.
In my case, I have a named route, which based on a parameter selected by the user, can render 3 different views. Of course, I have a default case for the first enter in this route, when the user doesn't selected any option yet.
When I use URL:previous(), Laravel take me back to the default view, even if the user has selected some other option. Only using javascript inside the button I accomplished to be returned to the correct view:
Voltar
I'm tested this on Laravel 5.3, just for clarification.
The following is a complete Blade (the templating engine Laravel uses) solution:
{!! link_to(URL::previous(), 'Cancel', ['class' => 'btn btn-default']) !!}
The options array with the class is optional, in this case it specifies the styling for a Bootstrap 3 button.
On 5.1 I could only get this to work.
Back
You can use javascript for this provblem.
It's retrieve link from browser history.
<script>
function goBack() {
window.history.back();
}
</script>
<button onclick="goBack()">Go Back</button>
One of the below solve your problem
URL::previous()
URL::back()
other
URL::current()
You can use {{ URL::previous() }}
But it not perfect UX.
For example, when you press F5 button and click again to Back Button with {{ URL::previous() }} you will stay in.
A good way is using {{ route('page.edit', $item->id) }} it always true page you wanna to redirect.
<i class="fa fa-angle-left"></i> Continue Shopping
This worked in Laravel 5.8

Categories