Laravel Backpack:从相关表格中获取数据

时间:2017-09-27 09:03:34

标签: laravel laravel-5.4 relationship crud backpack-for-laravel

我有类似结构的DB(我没有显示完整的结构):

users (id, name, comments)
company_user (id, company_id, user_id)
companies (id, name)
company_distributor (id, company_id, distributor_id)
distributors (id, name)

我有关系:

公司:

public function users()
    {
        return $this->belongsToMany('App\User');
    }

public function distributors()
    {
        return $this->belongsToMany('App\Models\Distributor')->withPivot('id')->whereNull('company_distributor.deleted_at');
    }

CompanyDistributor:

public function company()
    {
        return $this->belongsTo('App\Models\Company', 'company_id', 'id');
    }

public function distributor()
    {
        return $this->belongsTo('App\Models\Distributor');
    }

当我试图获得评论'来自用户 - 我得到空数据,但请求是正确的:

select users.*,company_user.company_id as pivot_company_id,company_user.user_id as pivot_user_id from users inner join company_user on users.id=company_user.user_id where company_user.company_id in ('1', '2', '3', '4', '7', '9', '10'........)

CompanyDistributorCrudController中的Crud:

$this->crud->addColumn([
            // 1-n relationship
            'label' => "Comments", 
            'type' => "select_multiple",
            'name' => 'company_id', 
            'entity' => 'company.users', 
            'attribute' => "comments", 
            'model' => "App\Models\Company", 
        ]);

出了什么问题?

当我尝试添加与CompanyDistributor的关系时:

public function users()
    {
        return $this->company->belongsToMany('App\User');
    }

sql查询获取' id'来自' company_distributor'表但不是' company_id'我需要的。

抱歉我的英文

0 个答案:

没有答案