简体   繁体   English

通过Laravel中的多对多关系模型对雄辩模型进行排序

[英]Sorting eloquent model by many-to-many relationship model in Laravel

I'm building an application on Laravel 5.8 where I'm having a model named company , technical_description , state , region . 我正在Laravel 5.8上构建一个应用程序,我有一个名为companytechnical_descriptionstateregion A company can have many locations in many state, so company and state/region are having many-to-many relationship. 公司可以在许多州拥有许多地点,因此公司和州/地区具有many-to-many关系。 So my model looks something like this: 所以我的模型看起来像这样:

class Company extends Model {

    use SoftDeletes;

    protected $guarded = [];

    public function states()
    {
        return $this->belongsToMany('App\State', 'company_state', 'company_id', 'state_id');
    }

    public function regions()
    {
        return $this->belongsToMany('App\Region', 'company_region', 'company_id', 'region_id');
    }


    public function technicalDescription()
    {
        return $this->hasOne('App\TechnicalDescription', 'company_id', 'id');
    }

}

I'm now building a data-table , where I'm having Company Name , Technical Details , State , Region as table headers. 我现在正在构建一个data-table ,我将Company NameTechnical DetailsStateRegion作为表格标题。 I want to sort according to these relationships, so I tried in controller: 我想根据这些关系排序,所以我在控制器中尝试:

public function companies() {
    return CompanyResource::collection(
        Company::when($request->name, function ($q) use($request) {
            $q->where('name', 'like', '%' . $request->name .'%');
        })
        ->join('company_technical_description', 'companies.id', '=', 'company_technical_description.company_id')
        ->when($request->sort_by_column, function ($q) use($request) {
            $q->orderBy($request->sort_by_column['column'], $request->sort_by_column['order'] );
        }, function ($q) use($request){
            $q->orderBy('updated_at', 'desc');
        })
        ->paginate();
    )
}

So in this case if I want to sort with any details of technical description it will be easy by simply pushing company_technical_decription.established_date in $request->sort_by_column['column'] 因此,在这种情况下,如果我想对技术描述的任何细节进行排序,只需在$request->sort_by_column['column']推送company_technical_decription.established_date

But in case of many-to-many relationship of state and region , I'm little stuck on how can I proceed. 但是,如果stateregion之间存在多对多的关系,那我就不知道如何进行。

Help me out with it. 帮助我解决它。 Thanks 谢谢

I know way with join , worked for me with dynamic order for data tables. 我知道join方式,为我工作的数据表的动态订单。

You can try something like this: 你可以尝试这样的事情:

public function companies() {
    return CompanyResource::collection(
        Company::when($request->name, function ($q) use($request) {
            $q->where('name', 'like', '%' . $request->name .'%');
        })
        ->select('companies.*', 's.name as state_name')
        ->join('company_technical_description', 'companies.id', '=', 'company_technical_description.company_id')
        ->join('company_state as cs', 'company_state.company_id', '=', 'companies.id')
        ->join('states as s', 'cs.state_id', '=', 's.id')
        ->when($request->sort_by_column, function ($q) use($request) {
            $q->orderBy($request->sort_by_column['column'], $request->sort_by_column['order'] );
        }, function ($q) use($request){
            $q->orderBy('updated_at', 'desc');
        })
        ->paginate();
    )
}

I didn't test this solution but it's good for start. 我没有测试这个解决方案,但这对开始很有帮助。 If you now pass state_name as argument for ordering, ordering should work. 如果您现在将state_name作为参数传递以进行排序,则排序应该有效。 But I'm not sure in your database tables and columns so set your join to work. 但我不确定您的数据库表和列,所以设置您的join工作。

I think it's good idea. 我认为这是个好主意。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM