Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vladjen's avatar

Request Implode in query

I am passing my form values to my controller function and i am filtering with the request values and returning to my view.


$posts = DB::table('posts')
                 ->join('images', 'posts.id', '=', 'images.post_id')
                 ->select('images.*','posts.*')
                 ->where(array('posts.user_type' => 'isUser',
                               'posts.status' => '2',
                               'images.position' => '1',
                               'posts.'.$request->condition => $request->condition))  //how do i overcome this ? 
                 ->orderBy('posts.created_at','desc')
                 ->get();

request->condition instead of that if i place my condition the query works of course. I have more than 5 different values in the form so i am trying to achieve this getting the request value straightaway. Please show me a way or suggest me a way.

0 likes
3 replies
Vladjen's avatar
<ul class="pl-2">
                <li>
                  <div class="form-check">
                    <label class="form-check-label">
                      <input type="checkbox" class="form-check-input" name="deskCondition" value="brandnew">Brandnew
                    </label>
                  </div>
                </li>
                <li>
                  <div class="form-check">
                    <label class="form-check-label">
                      <input type="checkbox" class="form-check-input" name="deskCondition" value="reconditioned">Reconditioned
                    </label>
                  </div>
                </li>
                <li>
                  <div class="form-check">
                    <label class="form-check-label">
                      <input type="checkbox" class="form-check-input" name="deskCondition" value="used">Used
                    </label>
                  </div>
                </li>
              </ul>

like this there are multiple request values in my form. I am posting via ajax request @sti3bas

Sti3bas's avatar

@vladjen not sure if I fully understand your problem, but you can apply optional filter with when method:

->when($request->deskCondition, function($query, $filters) {
   return $query->whereIn('posts.deskCondition', $filters);
});

Please or to participate in this conversation.