Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Automatically deleting related rows in Laravel (Eloquent ORM)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.02k
    Comment on it

    I am using Laravel and I want Automatically deleting rows using Eloquent ORM .In laravel we have deleting event .By using this "deleting" event we can do all the clean up or what we want to delete we are able to delete.

    By using example that will explain in better way:

    class User extends Eloquent
    {
        public function photos()
        {
            return $this->has_many('Photo');
        }
    
        // this is a recommended way to declare event handlers
        protected static function boot() {
            parent::boot();
    
            static::deleting(function($user) { // before delete() method call this
                 $user->photos()->delete();
                 // do the rest of the cleanup...
            });
        }
    }
    

    Note:You should probably also put the whole thing inside a transaction, to ensure the referential integrity..

    By this way we can achieve Automatically deleting rows using Eloquent ORM.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: