【问题标题】:Laravel multiple table relation using eloquent relationshipsLaravel 使用 eloquent 关系的多表关系
【发布时间】:2018-07-31 19:40:20
【问题描述】:

我有以下表格结构并尝试使用以下表格之间的雄辩关系,但无法理解如何应用。

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

基于上述代码,我正在获取用户和 tbl_users_business 详细信息,但现在我想获取 tbl_master_business_types、tbl_users_business_document 上的 business_id 列,请您指导我们。

表格:

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'
    ];
}

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    首先,您不需要 users 表上的 business_id。

    关系:

    用户 hasMany() 或 hasOne() 用户业务, UserBusiness belongsTo() MasterBusinessTypes,
    UserBusiness hasMany() UsersBusinessDocument

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

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 2015-05-19
      • 2019-06-13
      • 2015-01-01
      • 2021-05-09
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      相关资源
      最近更新 更多