8

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.

vignesh pbs
  • 418
  • 2
  • 5
  • 16
  • 1
    `category::find($name)` is the problem here. Are you sure it's returning a valid response? Do a `dd(category::find($name))` see what it spits out. – Andrei Jul 23 '15 at 12:22
  • its returning null as a output – vignesh pbs Jul 23 '15 at 12:23
  • 1
    Well there's your problem. Odds are `$name` doesn't exist in the database. Make sure it's there. And for future cases do something like `$record = category::find($name); if(null !== $record){//do stuff here}` – Andrei Jul 23 '15 at 12:25
  • but i am having a field name as name in database – vignesh pbs Jul 23 '15 at 12:26

3 Answers3

21

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();
Jerodev
  • 32,252
  • 11
  • 87
  • 108
0

In your view: it has to be a form to submit a POST request like below:

    <form  action="/deletecat/{{$data->id}}" method="post">
            @csrf
            @method('DELETE')
        <input type="submit" value="delete " >
    </form>

Because this is POST Request has to use csrf and delete action.

In your route, it has be with delete() method,followwing with the controller.

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

In the controller:

 public function destory($name)
{

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

    return Redirect::route('managecategory');

}

This is how laravel name format role: enter image description here

li bing zhao
  • 1,388
  • 13
  • 12
  • Please share more details - why should changing the name of that method help? Is there any magic behind naming the routes? – Nico Haase Feb 19 '21 at 07:02
  • It is how laravel name the action and controller, I guess it will be easy to cooperate with other developers. – li bing zhao Feb 19 '21 at 13:57
  • 1
    This would certainly make the code more standard, but it doesn't explain the problem they were having, which was given an answer 6 years ago. – miken32 Feb 19 '21 at 14:57
0

The error is obviously because of the returned NON-OBJECT.

::find($name) returns an empty object or null

This is because the search was empty.

Make sure name is the primary key in order to use find.

Otherwise:

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

or:

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

*I pointed = because if you ever need to search different than = use >= <> and so on...

Secondly, it is not very important to use destroy. It is recommended but not a must-have!

MaxiGui
  • 6,190
  • 4
  • 16
  • 33