简体   繁体   中英

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 . A company can have many locations in many state, so company and state/region are having many-to-many relationship. 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. 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']

But in case of many-to-many relationship of state and region , I'm little stuck on how can I proceed.

Help me out with it. Thanks

I know way with join , worked for me with dynamic order for data tables.

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. But I'm not sure in your database tables and columns so set your join to work.

I think it's good idea.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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