I will destroy my user with a HTML link, but it doesn't seem to generate the correct link, what am i doing wrong?
public function destroy($id) { //Slet brugeren $e = new User($id); $e->destroy(); //Log også brugeren ud Auth::logout(); //redrect til forsiden Redirect::to("users/create"); }
In my view i call this {{URL::action('UserController@destroy', array($user->id))}}
Just go into PHPMyAdmin at localhost/phpmyadmin/ and create a new Database using the “New” option. We are going to name it stocks for this tutorial but you can use any name you want. Now open the . env file on the root of your project and set the Database information.
Create, Read, Update and Delete are the very basic operations that almost every application has. Creating a basic CRUD Operations in laravel is very simple thing in Laravel.
Update 08/21/2017 for Laravel 5.x
The question asks about Laravel 4, but I include this in case people looking for Laravel 5.x answers end up here. The Form helper (and some others) aren't available as of 5.x. You still need to specify a method on a form if you are doing something besides GET or POST. This is the current way to accomplish that:
<form action="/foo/bar" method="POST"> <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <!-- other inputs... --> </form>
You can also use {{ method_field('PUT') }}
instead of writing out the hidden _method
input.
See https://laravel.com/docs/5.4/routing#form-method-spoofing
Original Answer for Laravel 4
I think when you click the link, it is probably sending a GET request to that end point. CRUD in Laravel works according to REST. This means it is expecting a DELETE request instead of GET.
Here's one possibility from a tutorial by Boris Strahija.
{{ Form::open(array('route' => array('admin.pages.destroy', $page->id), 'method' => 'delete')) }} <button type="submit" class="btn btn-danger btn-mini">Delete</button> {{ Form::close() }}
This way, you send the request in a form with the DELETE method. The article explains why a traditional link won't work:
You may notice that the delete button is inside a form. The reason for this is that the destroy() method from our controller needs a DELETE request, and this can be done in this way. If the button was a simple link, the request would be sent via the GET method, and we wouldn’t call the destroy() method.
An cool ajax solution that works is this:
function deleteUser(id) { if (confirm('Delete this user?')) { $.ajax({ type: "DELETE", url: 'users/' + id, //resource success: function(affectedRows) { //if something was deleted, we redirect the user to the users page, and automatically the user that he deleted will disappear if (affectedRows > 0) window.location = 'users'; } }); } } <a href="javascript:deleteUser('{{ $user->id }}');">Delete</a>
And in the UserController.php we have this method:
public function destroy($id) { $affectedRows = User::where('id', '=', $id)->delete(); return $affectedRows; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With