Dynamic method to pass Laravel Eloquent query parameters
P粉879517403
P粉879517403 2023-09-01 00:20:04
0
1
451
<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>
P粉879517403
P粉879517403

reply all(1)
P粉600402085

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

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

Since you used -> 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')])';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template