lundi 28 septembre 2015

Laravel boot method to delete relationship tables

I have the following boot method setup in my user model:

/**
 * Boot the model.
 *
 */
public static function boot()
{
    parent::boot();

    static::deleting(function($user)
    {
        $user->roles()->detach();
        $user->supervisors()->detach();
        $user->types()->detach();
        $user->rates()->detach();
        $user->miscs()->detach();
        $user->timesheets()->delete();
    });
}

A user can have many timesheets so I have a hasMany relationship setup for timesheets in the user model.

/**
 * The timesheets that belong to the user.
 *
 * @return Object
 */
public function timesheets()
{
    return $this->hasMany('App\Models\Timesheet\Timesheet');
}

A timesheet can also have many data, so I have added a relationship in the timesheet model:

/**
 * The data that belong to the timesheet.
 *
 * @return Object
 */
public function data()
{
    return $this->hasMany('App\Models\Timesheet\DataTimesheet');
}

My question is, when I delete a user, it should delete the timesheet and all data associated with it. At the moment, when I delete a user it deletes the user and the timesheet but not the data (DataTimesheet).

When I delete a timesheet separately, the data gets deleted.

Can anyone suggest how I can delete the data as well when a user is deleted?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1LgGJog
via IFTTT

Aucun commentaire:

Enregistrer un commentaire