Dynamic method to pass Laravel Eloquent query parameters
P粉879517403
2023-09-01 00:20:04
<p>I have the following query parameters in string format</p>
<pre class="brush:php;toolbar:false;">$query = '->whereIn('short_code', ["9999"])->whereBetween('request_timestamp', [request(' startTime'), request('endTime')])';</pre>
<p>How do I pass it to Eloquent? I'm trying to achieve something like this</p>
<pre class="brush:php;toolbar:false;">InboundMessage::query()->{$query};</pre>
<p>I got the following error</p>
<pre class="brush:php;toolbar:false;">Properties[->whereIn('short_code', ["9999"])->whereBetween('request_timestamp', [request('startTime' ), request('endTime')])] does not exist on the Eloquent builder instance. </pre>
The problem with the above query is that it looks like this
Since you used
->
in both the query builder and the $query string. So just adjust your $query to