Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel one to many relationship example

$user->roles()->attach($roleIds);
$user->roles()->detach($roleIds);
$user->roles()->sync($roleIds);
$user->roles()->toggle($roleIds);
Comment

1 to many relationship in Laravel

class Post extends Model
{
   // 1 Post has many comments
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
Comment

many to many relationship laravel

use AppModelsUser;

$user = User::find(1);

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

many to many relationship laravel

// in User model:
<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles()
    {
        return $this->belongsToMany('AppRole');
    }
}

// in Role model:
<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('AppUser')->using('AppUserRole');
    }
}
Comment

laravel many to many relationship

/*
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);
    }
}
Comment

many to many relationship laravel example

<?php namespace App; use IlluminateDatabaseEloquentModel; class UserRole extends Model{     }
Comment

PREVIOUS NEXT
Code Example
Php :: laravel withcount change name 
Php :: create file in directory php 
Php :: laravel allow all fillable 
Php :: PHP code to read JSON string on server 
Php :: laravel return from db reorder 
Php :: render html data from db laravel 
Php :: custom blade if directive 
Php :: php check if type is mysqli_result 
Php :: setup phpmyadmin to show create statement query 
Php :: how to check page loading time in php 
Php :: laravel form request validation api 
Php :: drupal show php errors 
Php :: php get parent url from script location 
Php :: add a snippet in twig shopware 6 
Php :: livewire model array 
Php :: remove php 
Php :: check count in laravel 
Php :: php post not working 
Php :: Laravel artisan command to create model plus migration 
Php :: php website templates free download with database 
Php :: how to deploy php website on server 
Php :: laravel route regex except 
Php :: Using $this when not in object context 
Php :: Laravel Excel check if column exists 
Php :: array map php 
Php :: wordpress wp_nav_menu custom id 
Php :: strcmp php 
Php :: PHPDoc @method 
Php :: php functions 
Php :: get from link php 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =