Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Fetch pivot data laravel

class User extends Eloquent
{
    public function Account() { return $this->hasOne('Account', 'user_id'); }
    public function AccountRoles() { return $this->belongsToMany('Role', 'account_role')->withPivot('account_id')->withTimestamps(); }
    public function Roles() { return $this->belongsToMany('Role', 'role_user')->withTimestamps(); }
}
    
class Account extends Eloquent
{
    public function User() { return $this->belongsTo('User', 'user_id'); }
    public function AccountRoles() { return $this->belongsToMany('Role', 'account_role')->withPivot('user_id')->withTimestamps(); }
}
    
class AccountRole extends Eloquent
{
    public function Account() { return $this->belongsTo('Account', 'account_id'); }
}


////////////////////////////////////////////////////////////////////////

$user = User::find(1);

foreach($user->AccountRoles as $account_role)
{
    echo 'Account ID: ' . $account_role->pivot->account_id . '<br/>'; // Correctly spits out the account_id
    echo 'Role: ' . $account_role->role_name . '<br/>'; // correctly spits out the role name
}
Comment

PREVIOUS NEXT
Code Example
Php :: php array remove the last element 
Php :: create a button add in laravel 
Php :: how to integrate google reCAPTCHA in codeigniter? 
Php :: laravel edit method 
Php :: php array sort 
Php :: create qr code png image of 200*200 using phpqrcode 
Php :: curlopt_postfields php example 
Php :: Csv To AssoT Php 
Php :: remove field from object php 
Php :: The Process class relies on proc_open, which is not available on your PHP installation cpanel 
Php :: cakephp sql query 
Php :: onclick on image php 
Php :: iqbal Laravel save record in two table in one line 
Php :: the plugin generated 14 characters of unexpected output during activation. if you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin 
Php :: get id from object 
Php :: Logging a Massage php 
Php :: php get last 3 elements of array 
Php :: laravel all() 
Php :: wordpress get all published post 
Php :: validate file exist php 
Php :: displaying dates using php code 
Php :: php inner join array 
Php :: laravel blade for if 
Php :: how to make a variable in php 
Php :: @can in laravel 
Php :: symfony 3.4 cache control 
Php :: create xml php 
Php :: php override built in functions 
Php :: Not Found The requested URL was not found on this server. Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11 Server at localhost Port 80 
Php :: php how to check if user has a role on login 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =