Laravel: dynamic where clause with Elouquent

前端 未结 6 1224
温柔的废话
温柔的废话 2021-02-05 03:44

I am calling URL with search params which are dynamic. How could I form proper Eloquent query?

In theory:

  1. query
  2. query whe
6条回答
  •  有刺的猬
    2021-02-05 04:14

    It's easy with Laravel. Just do something like this:

    $query = User::query();
    
    if ($this == $that) {
      $query = $query->where('this', 'that');
    }
    
    if ($this == $another_thing) {
      $query = $query->where('this', 'another_thing');
    }
    
    if ($this == $yet_another_thing) {
      $query = $query->orderBy('this');
    }
    
    $results = $query->get();
    

提交回复
热议问题