Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 Delete a row from database

I am trying to delete a category by clicking on a button

Blade:

<td class="center"><a href="{{URL::to('/deletecat/'.$category->name) }}"><span class="glyphicon glyphicon-trash"></span></a></td>

Route:

Route::get('/deletecat/{name}','CategoryController@delete');

Controller:

 public function delete($name)
    {

        category::find($name)->delete();

        return Redirect::route('managecategory');

    }

but I am getting an error while clicking on a button that

Call to a member function delete() on a non-object

Any help appreciated.

like image 873
vignesh pbs Avatar asked Jul 23 '15 12:07

vignesh pbs


1 Answers

The ::find($id) method expects $id to be a number, the primary key of the row you want to find.

If you want to delete a row by name, you should use the following code:

category::where('name', $name)->delete();
like image 75
Jerodev Avatar answered Sep 17 '22 20:09

Jerodev