mercredi 30 janvier 2019

Laravel QueryBuilder where clause with user info on other model

I have a Model ImageRequest that is related to my User model like this:

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

An my User model like this:

public function imageRequests()
{
    return $this->hasMany('App\ImageRequest');
}

Now I use this package to build my filterable query to fetch all ImageRequests:

spatie/laravel-query-builder

this is what my query looks like:

$query = QueryBuilder::for(ImageRequest::class)
        ->with(['requestTypes'])
        ->allowedIncludes('requestTypes')
        ->orderByRaw("FIELD(status , 'new') desc")
        ->orderBy('functional_id', 'asc')
        ->allowedFilters(
            'id',
            'pv_number',
            'created_at',
            Filter::scope('starts_between'),
            'location_of_facts',
            'train_nr',
            'status',
            'result_of_search',
            Filter::custom('type', RequestTypeFilter::class)
);

I need to add a where clause for the User model something like this:

->where('zone', Auth::user->zone);

But it says:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'zone' in 'where clause' (SQL: select count(*) as aggregate from image_requests where zone = a)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire