Laravel beaucoup à beaucoup de relations

Exemples de code

7
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();
4
0

Où est le site: https: / / laravel. com / docs/

use Illuminate\Database\Eloquent\Builder;

// Retrieve posts with at least one comment containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();

// Retrieve posts with at least ten comments containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
}, '>=', 10)->get();
1
0

beaucoup à beaucoup de relations laravel

use App\Models\User;

$user = User::find(1);

$user->roles()->attach($roleId);
0
0

laravel beaucoup à beaucoup de relations

/*
users
    id - integer
    name - string

roles
    id - integer
    name - string

role_user
    user_id - integer
    role_id - integer
*/

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
      /*To determine the table name of the relationship's intermediate 
      table, Eloquent will join the two related model names in 
      alphabetical order. However, you are free to override this 
      convention. You may do so by passing a second argument to the 
      belongsToMany method*/
        return $this->belongsToMany(Role::class,'role_user');
    }
}
//Defining The Inverse Of The Relationship

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}
0
0

laravel comment interroger la relation belongsTo

$movies = Movie::whereHas('director', function($q) {
    $q->where('name', 'great');
})->get();

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
..................................................................................................................