
Delete records with relationship in laravel
Created at 20-Mar-2021 ,
By samar
Delete records with relationship in laravel
Good day, guys. In this post, we’ll look at how to solve the "Delete records with relationship in laravel" programming puzzle.
-
--PATH app\Http\Controllers\<YourController.php>
$post = Post::find(1); $post->comments()->delete();
Delete records with relationships in laravel is very simple as record creation. Find out the record and with the help of the delete() method deletes all the associated records. Like if you want to delete all comments related to a particular post this code snippets do it for you. -
--PATH app\Models\Post.php
public function comments() { return $this->hasMany('App\Models\Comment'); } public static function boot() { parent::boot(); self::deleting(function($post) { $post->comments()->each(function($comment) { $comment->delete(); }); }); }
This code snippet can delete all the associated comments using the deleting event on delete a post model. Events allow you to easily execute code each time a specific model class is saved, updated or deleted in the database. To delete a model directly, call delete() on it and don't define a deleting listener in its boot method or define an empty deleting method. If you want to further delete relations of a related model, you can define a deleting listener in the boot method of that model and delete the relations there.
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
- Print last executed query in laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Create a record if not exist in laravel
- How to pass data to multiple partial view files in laravel
- How to check column value of a record is null or not in laravel
- How to call model in blade laravel
- Submit form without CSRF token in Laravel
- FirstOrCreate() Not Inserting Model
- Laravel order by date not working
- Ignore Records where a field has NULL value in Laravel
- How to Get records between two dates in Laravel
- Get Array of IDs from Eloquent Collection
- How to restore multiple records after soft-deletes in Laravel
- Create project table with model and migration
- How to insert dynamic value to additional column in pivot table in laravel
- Insert values in pivot table dynamically in laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Laravel get single row by id
- How to create static page in Laravel
- How to create projects method with belongstomany relationship in user model
- How to check record exist or not in relationship table
- How to get CSRF token in laravel controller
- Convert input array to comma-separated string in laravel controller
- How to get date from created_at field in laravel
- How to pass data to route in laravel?