Php 如何制作类似codeigniter的laravel查询生成器活动记录

Php 如何制作类似codeigniter的laravel查询生成器活动记录,php,laravel,laravel-4,query-builder,Php,Laravel,Laravel 4,Query Builder,我对Laravel4查询生成器有一个问题,我想创建一个可重用的方法 public function getData($where=array()) { // $where = array('city' => 'jakarta', 'age' => '25'); return User::where($where)->get(); // this will produce an error, because i think laravel didn't

我对Laravel4查询生成器有一个问题,我想创建一个可重用的方法

public function getData($where=array())
{
    // $where = array('city' => 'jakarta', 'age' => '25');
    return User::where($where)->get();

    // this will produce an error, because i think laravel didn't support it
}
在CodeIgniter中,很容易将数组传递到活动记录:

public function getData($where=array())
{
    $rs = $this->db->where($where)->from('user')->get();

    return $rs->result();
}

// it will produce :
// SELECT * FROM user WHERE city = 'jakarta' AND age = '25'
知道如何在Laravel 4查询生成器上使用它吗?我在谷歌上搜索过,但没有找到任何答案。之前谢谢。

您可以尝试此功能(假设此功能在您的
用户
型号中)

$where[] = array(
   'field' => 'city',
    'operator' => '=',
    'value' => 'jakarta'
);
$where[] = array(
    'field' => 'age',
    'operator' => '=',
    'value' => 25
);
$data = getData($where);

public function getData($wheres = array()){

    $query = User::query();
    if(!empty($wheres)){
       foreach($wheres as $where){
        {
            $query = $query->where($where['field'], $where['operator'], $where['value']);
        }
    $result = $query->get();
    }

}
请记住,
=
是可选的。就像

$data = User::getData(array('first_name' => 'Jhon'));
您可以尝试此功能(假设此功能在您的
用户
型号中)

请记住,
=
是可选的。就像

$data = User::getData(array('first_name' => 'Jhon'));

谢谢你的回答,兄弟。。。拉威尔·赛尔夫没有办法处理这种情况吗?谢谢你的回答,兄弟。。。Laravel self是否没有处理这种情况的方法?目前还没有任何方法,但最近提交了一个请求。您可以在此处查看并说出您的想法:。目前还没有任何方法可以执行此操作,但最近提交了一个请求。您可以在此处查看并说出您的想法:。