Delete all related comments on deleting a post in Laravel
You can delete all the related comments to a post in laravel using the deleted event.
In Laravel, the deleted event trigger can be used to perform additional actions related to a deleted record. For example, if you have a Post model with a relationship to a Comment model, you can use the deleted event trigger to automatically delete all related comments when a post is deleted.
To delete the comments table records related to posts table record using deleted event, you have to create the posts table with id, title, slug ,body, created_at, updated_at columns and comments table with id, post_id, body, created_at and updated_at columns where post_id is the foreign key related to posts table id column. You also have to create the relationship between two tables (posts and comments) using hasMany relationship in the Post model.
app\Models\Post
class Post extends Model
{
use HasFactory;
protected $table = "posts";
protected $fillable = [
'title', 'slug', 'body', 'created_at', 'updated_at'
];
protected static function boot(){
parent::boot();
static::deleted (function($post){
$post->comments()->delete();
});
}
public function comments(){
return $this->hasMany (Comment::class);
}
}
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Models\Post;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
Post::whereId(1)->first()->delete();
echo "Post with comments deleted";
});
Manually delete the comments table records
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Models\Post;
use App\Models\Comment;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$postId = 1;
Post::whereId($postId)->delete();
Comment::wherePostId($postId)->delete();
echo "deleted";
});
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Insert data with form validation using ajax in laravel
- How to show data by ID in laravel?
- Composer create project laravel/laravel example app
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- On delete set foreign id column value null using migration in laravel 8
- How to pass data to multiple partial view files in laravel
- Call to a member function update() on null
- How to validate URL with https using regex in laravel
- Get current URL on visit URL in Laravel
- Pass value from controller to model in laravel
- Laravel create default admin user
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Sample .htaccess file and index.php file under public directory in laravel
- Multiple Level eager loading in Laravel
- How to check relationship is loaded or not in Laravel
- Array to string conversion laravel Controller
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Create a record if not exist in laravel
- Get last year created records in Laravel
- How to fetch single row data from database in laravel
- Laravel form request validation
- How to disable timestamps in laravel
- Comment .env file in laravel
- Input file with max size validation in laravel