vendredi 20 septembre 2019

Laravel Eloquent How can I select and manipulate rows in pivot table?

I have three database tables called user(id,name), group(id,name) and user_group(user_id, group_id,valid_before) with relations many to many.

class User extends Model
{
    protected $table = 'user';

    public function groups()
    {
    return $this->belongsToMany(Group::class, 'user_group')
        ->withPivot('valid_before');
    } 
}

class Group extends Model
{
    protected $table = 'group';

    public $timestamps = false;

    public function user()
    {
        return $this->belongsToMany(User::class, 'user_group');
    }
}

User can be member of some group until time moment 'valid_before'. If 'valid_before' is less than current time, the user must be excluded from the group and corresponding row in pivot table must be deleted. How can do it using Eloquent?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30bPeyx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire