Laravel 5.1 Deleting 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 when clicking a button which

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

      

Any help was appreciated.

+3


source to share


1 answer


The method ::find($id)

expects to $id

be a number, the primary key of the string you want to find.

If you want to delete a line by name, you must use the following code:



category::where('name', $name)->delete();

      

+2


source







All Articles