
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
- Extra Filter Query on Relationships in Laravel
- Ajax GET request in laravel
- How to insert ckeditor data into database in Laravel?
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- How to remove package from laravel
- How to insert multiple rows in mysql using loop in laravel?
- Get 30 days older records from table in laravel
- Target class [admin] does not exist.
- How to avoid duplicate entries in pivot table in Laravel
- Trying to access array offset on value of type null error in laravel
- How to validate website url in laravel using validaiton
- How to remove P tag from CkEditor in Laravel?
- Import/Use Storage facade in laravel
- Laravel onclick function not working
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to get laravel errors folder in views directory in laravel
- How to pass data to multiple partial view files in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- How to decrypt laravel password
- Submit form without CSRF token in Laravel
- Get id of last inserted record in laravel
- Get latest record by created at in Laravel
- How to pass variable from controller to model in Laravel
- Call to a member function update() on null
- How to create controller in laravel