我具有以下表结构,并试图在波纹管表之间使用雄辩的关系,但无法理解如何应用。
$user = User::find('4')->load(['usersBusinessWeb']);
return View::make('admin.dashboard')->with('user', $user);
基于上述代码,我正在获取用户和tbl_users_business详细信息,但现在我想在business_id列上获取tbl_master_business_types,tbl_users_business_document,以便您可以指导我们。
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
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('App\UsersBusiness', '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'
];
}
答案 0 :(得分:0)
首先,您不需要在users表上使用business_id。
关系:
用户hasMany()或hasOne()UserBusiness, UserBusiness属于()MasterBusinessTypes,
UserBusiness hasMany()UsersBusinessDocument
$business = $user->business->find($id);
$business->businessType;
business->docs;