mardi 30 mai 2017

Laravel Eloquent accessing fields from another table in scope

I have two tables:

  1. Users:

id, first_name, last_name, email


  1. Employees

id, user_id, wage

There has a relationship between users and employees. The problem comes from when I have a scope inside the model:

public function scopeSearchPaginateAndOrder($query) {
        $request = app()->make('request');

        return $query->whereHas('user.internal_companies', function ($company) {

              $company->where('companies.id', '=', \Session::get('current_company')['id']);

        })->where(function ($query) use ($request) {


      })->select(['user.first_name'])->get();
}

I am getting the following error:

Unknown column 'user.first_name' in 'field list

However, the relationship does exist:

public function user() {
        return $this->belongsTo('App\User', 'user_id');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire