0

My Laravel Project have a resource controller named Blogpost, where in destroy method, the $blogpost->delete() approach doesn't work. But if I use DB Query, it works just fine. See code below.

This doesn't work:

public function destroy(Blogpost $blogpost)
{
    $blogpost->delete();
    
    return redirect()->route('blog.index');
}

This works perfectly:

public function destroy($id)
{
    $post = Blogpost::find($id); 
    $post->delete();

    return redirect()->route('blog.index');
}

In routes\web.php, I'm using resource route for this controller.

Route::resource('blog', BlogpostController::class);

Note: The Blogpost model have a hasMany() relationship with postViews model.

public function postViews()
{
    return $this->hasMany(PostViews::class);
}

Note: post_views table have a foreign key associate with blogpost table. See migration below:

$table->foreign('blogpost_id')
              ->references('id')
              ->on('blogposts')
              ->onDelete('cascade');

If I use dd($blogpost); it returns the model.

code:

public function destroy(Blogpost $blogpost)
{
    dd($blogpost);
}

Output:

dd() inside destroy method

My question is, why $blogpost->delete(); approach is not working? is it because of the hasMany() relationship?

PS: I have another resource controller named Category, where the $category->delete() approach works perfectly.

  • 2
    I guess your route parameter is called {id} and `$blogpost` is null? do you get an error? Try to `dd($blogpost)` – Gert B. Sep 20 '21 at 07:05
  • Hi @gert b, I'm using resource route. code: Route::resource('blog', BlogpostController::class); –  Sep 20 '21 at 07:08
  • did you `dd($blogpost)`? Do you get an error? – Gert B. Sep 20 '21 at 07:09
  • Hi @GertB. I just did dd($blogpost), it returns App\Models\Blogposts. See this screenshot: [link](https://i.imgur.com/GE40GYY.jpg) –  Sep 20 '21 at 07:16

2 Answers2

1

TL;DR Change your destroy method to be:

public function destroy(Blogpost $blog)
{
    $blog->delete();
    
    return redirect()->route('blog.index');
}

I would imagine the reason this is happening is because your route parameter is blog but your controller parameter is $blogpost i.e. they don't match.

If you type hint a controller method parameter, Laravel will try and resolve an instance of it out of the container. This is why you get an instance of Blogpost but not the loaded instance you want. Route model binding won't work here because the names have to match.

Rwd
  • 34,180
  • 6
  • 64
  • 78
0

You did't set model in controller. And your output in attributes and original is null, so you can't delete the post. Now, first remove controller and model then run below command and then try delete method:

php artisan make:controller BlogpostController --resource --model=Blogpost

enter image description here

Fefar Ravi
  • 794
  • 7
  • 18