search

Home  >  Q&A  >  body text

Dynamic method to pass Laravel Eloquent query parameters

<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粉879517403P粉879517403492 days ago471

reply all(1)I'll reply

  • P粉600402085

    P粉6004020852023-09-01 10:40:21

    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')])';

    reply
    0
  • Cancelreply