Home > Software design >  Passing laravel eloquent query params dynamically
Passing laravel eloquent query params dynamically

Time:09-16

I have the following query params in string format

$query =  '->whereIn('short_code', ["9999"])->whereBetween('request_timestamp', [request('startTime'), request('endTime')])';

How do i pass it to the eloquent? Am trying to achieve something like this

InboundMessage::query()->{$query};

Am getting the error below

Property [->whereIn('short_code', ["9999"])->whereBetween('request_timestamp', [request('startTime'), request('endTime')])] does not exist on the Eloquent builder instance. 

CodePudding user response:

The problem with the above query is that it looks like this

InboundMessage::query()->->whereIn('short_code', ["9999"])..

Since you have put -> in both the query builder and the $query string. So just adjust your $query to

$query = 'whereIn('short_code', ["9999"])->whereBetween('request_timestamp', [request('startTime'), request('endTime')])';

CodePudding user response:

It will be something like this (not tested) using raw DB expressions:

$query = DB::table('tableName')
->whereRaw('columnName IN ["9999"] AND columnName BETWEEN value1 AND value2')
->get();
  • Related