Laravel multiple table relation using eloquent relationships

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Laravel multiple table relation using eloquent relationships



I have following table structure and trying to use eloquent relationships between bellow tables but not able to understand how to apply.


$user = User::find('4')->load(['usersBusinessWeb']);
return View::make('admin.dashboard')->with('user', $user);



based on above code I am getting user & tbl_users_business details but now I want to fetch tbl_master_business_types, tbl_users_business_document on business_id column so can you please guide us.



Tables:


users
-----
user_id | business_id (p.k. of tbl_users_business) | first_name | last_name | email
4 | 1 | Samuel | Petersen | samuel_petersen@mailinator.com

------------------
tbl_users_business
------------------
business_id | user_id (p.k. of users) | business_type_id (p.k. of tbl_master_business_types) | business_name
1 | 4 | 3 | Charde Terry

-------------------------
tbl_master_business_types
-------------------------
business_type_id | business_type_name | description
1 | Movie | xxxxxxxxxxxxxx
2 | Hotel | xxxxxxxxxxxxxx
3 | Restaurant | xxxxxxxxxxxxxx

---------------------------
tbl_users_business_document
---------------------------
business_document_id | business_id (p.k. of tbl_users_business) | doc_path
1 | 1 | sample1.pdf
2 | 1 | sample1.pdf



Models:


User Model
----------
class User extends Authenticatable
{
use Notifiable;
use EntrustUserTrait;
use HasApiTokens;

protected $primaryKey = 'user_id';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'business_id', 'first_name', 'last_name', 'email'
];

public function usersBusinessWeb()
{
return $this->belongsTo('AppUsersBusiness', 'business_id');
}
}

-------------------
UsersBusiness Model
-------------------
class UsersBusiness extends Model
{
protected $primaryKey = 'business_id';

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tbl_users_business';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id', 'business_type_id', 'business_name'
];
}

-------------------------
MasterBusinessTypes Model
-------------------------
class MasterBusinessTypes extends Model
{
protected $primaryKey = 'business_type_id';

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tbl_master_business_types';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'business_type_name', 'description'
];
}

---------------------------
UsersBusinessDocument Model
---------------------------
class UsersBusinessDocument extends Model
{
protected $primaryKey = 'business_document_id';

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tbl_users_business_document';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'business_id', 'doc_path', 'doc_type'
];
}




1 Answer
1



First, you don't need business_id on users table.



Relations:



User hasMany() or hasOne() UserBusiness,
UserBusiness belongTo() MasterBusinessTypes,
UserBusiness hasMany() UsersBusinessDocument



$business = $user->business->find($id);

$business->businessType;
business->docs;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

LnekvCh8jeB,CvHRfC7X6r0bD5QtUWv
GWZUOaM e6 gH1Cj Q79ja JVAfG7,B

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Current 93