I am using Laravel 5 and I need to call a destroy Method inside my edit view.
All examples I found cover this separated with an index view and two buttons (one for editing and one for deleting).
Can somebody give me a hint to call a destroy Method inside an edit view?
You can put this code anywhere, not just in index view. Just create a link and use get route or use destroy route with form button to send a request:
{!! Form::open(['method' => 'Delete', 'route' => ['someroute.destroy', $id]]) !!}
<button type="submit">Delete</button>
{!! Form::close() !!}
Put this in the show view of the item
<form action="{{ route('posts.destroy', $post->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button class="btn btn-danger">Delete Post</button>
</form>
Related
Hey guys i have an error when i submit the delete it says that i'm not sending the id to the route i think,
that's the code to send the id to the route (and sorry for my english),
<form id="del_type" action="{{ route('admin.event.destroy',$event->id)}}" method="post">
{!! method_field('delete') !!}
{{ csrf_field() }}
<button class="btn btn-danger" type="submit" id="del_id">Supprimer</button>
it's wierd cause when i submit it's deleting the element from the database but with this error and in the URI i can see the id
take a look at the routes (All route names are prefixed with 'admin.')
Route::delete('evenment/event/{id}','EventController#destroy')->name('event.destroy');
change route to
admin.event.destroy
Change action to this and try
{{ route('admin.event.destroy', ['id' => $event->id])}}
I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.
Here is my code:
<form action="/admin/pages/delete/{{ $section->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-sm" type="submit">Delete</button>
</form>
What is a proper way to handle this?
It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.
Route definition inside admin group:
Route::delete('/pages/delete/{id}', 'PagesController#delete')->name('pages.delete');
I believe you are missing the csrf token in the form.
You can add
{{ csrf_field() }}
just after your form starts.
Visit this link for knowing more about csrf
You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller
{{ csrf_field() }} // add this before or after the {{ method_field() }}
I'm using Laravel and I'm trying to achieve a delete function that takes care of deleting the selected task the user made.
I was wondering how can I achieve this using routes?
<button class="btn btn-outline-light">Edit</button>
<button class="btn btn-outline-light">Delete</button>
At this moment my delete route takes me to the same page as what the edit route does.
My delete function:
public function destroy(Task $task)
{
$task->delete();
return redirect('/tasks')->with('success', 'Task removed');
}
I got a working delete function using the Laravel forms:
{!! Form::open(['action' => ['TasksController#destroy', $task->id], 'method' => 'POST', 'class'=> 'float-right']) !!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-outline-danger'])}}
{!! Form::close() !!}
Is it worth to consider another method of taking care of my deletes or should I stick with using the Laravel forms?
That's because of CSRF protection. You may think using an ajax request.
You can find an example in this post.
How to delete record in laravel 5.3 using ajax request?
When you press the delete link, you'll be basically making a GET request to the tasks.destroy route, which is not what you want. You can either create a form and submit it on click (the laravel logout btn does something similar)
<a href="#"
onclick="event.preventDefault(); document.getElementById('task-destroy-form').submit();">
Delete task
</a>
<form id="task-destroy-form" action="{{ route("tasks.destroy", ["id" => $task->id]) }}" method="POST" style="display: none;">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
</form>
or make an ajax call that will send the id and CSRF parameters to the route.
I am using the FOSUserBundle. Basically I overrided the registration controller and handler, and i created a customised registration form. What I want to do is to stay on the same page if there's an error during registration failure.
I managed to stay on the same page but the form reloads but doesn't show the error.
So basically how can I generate these errors while staying on the page without clearing the form fields.
Thank you very much for your help.
EDIT: Try this one. Call the form_errors so it prompts the error message within your registration page.
<form id="sign_up" action="{{ path('your_registration_route') }}" method="POST">
{{ form_errors(form) }}
{{ form_widget(form.username) }}
{{ form_errors(form.username) }}
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_errors(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.second) }}
<button class="btn" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}">SIGN UP</button>
{{ form_rest(form) }}
I'd like to display data calculated in controller in a blade view based on data provided by a user in form . For now I'm doing this by:
//View xyz.blade.php
<div class="card">
<div class="card-header">Add link</div>
<div class="card-block">
{{ Form::open(array('url' => 'getSimilar', 'method' => 'GET')) }}
{{ Form::token() }}
<div class="form-group">
{{ Form::label('url', 'url:') }}
{{ Form::text('url', '') }}
</div>
{{ Form::submit('Get') }}
{{ Form::close() }}
</div>
#if( ! empty($similar))
<div class="card-block">
#foreach ($similar as $item)
{{$item->title}} <br/>
#endforeach
</div>
#endif
</div>
//Controller
public function getSimilar(){
..
return View('xyz', ['similar' => $found]);
}
The above code works as expected. I can provide a url and after clicking "Get" I can see a list of found items below the form. However, something tells me that this is not the right way to do this since the entire page will refresh. Am I right (and so the above code is bad)? If so, is there any build in feature to display fetched data without refreshing? I searched on the official Laravel-form page but I did not find anything.
As far as I could understand your question.
The code seems to be ok, but the logic you've created may not be possible without a page refresh, because the user interaction depends the server response which requires a new reload.
You can craft another interactive action without a page refresh using an AJAX so when the user clicks the button he gets the result from the server you then display the results in page. Because the AJAX Request/Response happens behind the scenes for the user it gives an impression the page didn't reload which may be what you want.