Appartient à beaucoup avec pivot laravel

Exemples de code

6
0

un à plusieurs laravel

For example, a blog post may have an infinite number of comments. And a single
comment belongs to only a single post  

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo('App\Models\Post');
    }
}
5
0

laravel détacher

// Detach a single role from the user...
$user->roles()->detach($roleId);

// Detach all roles from the user...
$user->roles()->detach();
1
0

relation laravel beaucoup à beaucoup avec le tableau croisé dynamique

$user = User::find(1); //any user we want to find 
$user->trophies()->attach($idOfTrophy); 
//pass id or array of a Trophy ids 
//suppose admin has selected the trophy from a form and trophy id
// is in $request object, then.
$trophyId = $request->trophy_id;
$user->trophies()->attach($trophyId); //record is created in DB.
attach and syncWithoutDetaching both does same job
0
0

relation laravel beaucoup à beaucoup avec le tableau croisé dynamique

$user = User::find(1); //any user we want to find $user->trophies()->attach($idOfTrophy); //pass id or array of a Trophy ids //suppose admin has selected the trophy from a form and trophy id// is in $request object, then.$trophyId = $request->trophy_id;$user->trophies()->attach($trophyId); //record is created in DB.attach and syncWithoutDetaching both does same job

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................