jeudi 6 décembre 2018

Laravel query on relationship with joined table

I have a one-to-one relationship. My jobs table is connected to user_jobs table. I have a join on that relationship to get the users data that is connected to the job.

This is my relationship inside the Job model:

public function client(){
    return $this->hasOne('App\ClientJob')->leftJoin('clients', 'client_jobs.client_id', 'clients.id')
    ->select('clients.id', 'client_id', 'job_id', 'name');
}

This part of the code works and I get the clients data.

However I can't add a where clause on that relationship (specifically the name column that is fetched).

This is the part of my code that tries to search users by their name column:

$jobs = Job::
    when($request->input('keywords'), function($query) use ($request) {
        return $query->where(function ($query) use ($request) {
        $keywords = $request->input('keywords');
            $query->where('title', 'like', '%'.$keywords.'%');
            $query->orWhereHas('client', function ($query) use ($keywords){
                $query->where('name', 'like', '%'.$keywords.'%');
            });
            $query->orWhereHas('category', function ($query) use ($keywords){
                $query->where('name', 'like', '%'.$keywords.'%');
            });
            $query->orWhereHas('status', function ($query) use ($keywords){
                $query->where('name', 'like', '%'.$keywords.'%');
            });
        });
    })

This part is getting a "Column no found" error.

$query->orWhereHas('client', function ($query) use ($keywords){
                $query->where('name', 'like', '%'.$keywords.'%');
            });

Anyone know how to get this query up and running apart from removing the join inside the model and moving it into the query above the where clause?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2E3HOkF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire