Can you use query builder to build a query with a dynamic WHERE clause at runtime in laravel?

后端 未结 2 603
醉梦人生
醉梦人生 2021-01-25 02:16

So I know if I build a dynamic SQL string I can do something like the following

$SQL = \"SELECT * FROM \" . $table;

$first = 1;
foreach($items as $key => $va         


        
相关标签:
2条回答
  • 2021-01-25 02:41
    $query = $tableModel->newQuery();
    
    foreach($items as $key => $val) {
        $query->where($key, "LIKE", '%'.$val.'%');
    }
    
    0 讨论(0)
  • 2021-01-25 02:46
    foreach( $items as $key => $value )
    {
        $query->where( $key, '=', $value );
    }
    

    Is something like that what you're after?

    EDIT: Looks like Mark beat me to it.

    0 讨论(0)
提交回复
热议问题