Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRUD Laravel 4 how to link to destroy?

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))}}

like image 268
helloworld Avatar asked Oct 28 '13 19:10

helloworld


People also ask

How do you implement CRUD create read update and delete operations in laravel?

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.

What is crud in laravel?

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.


2 Answers

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.

like image 156
salsbury Avatar answered Oct 03 '22 16:10

salsbury


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; } 

 

like image 22
paulalexandru Avatar answered Oct 03 '22 17:10

paulalexandru