static function getall($input) {
$where = [];
$params = [];
$sql = 'SELECT * FROM radio_city';
if (isset($input['city']) && $input['city']) {
$where[] = ' city = ?'; // Subsequent additions to $where should specify AND/OR conditional
$params[] = $input['city'];
}
$sql .= implode(' ', $where);
$sql .= " GROUP BY city";
return DB::select($sql, $where, $params);
}
}
在这里,我使用的是Laravel(MVC)框架。我正在从控制器重定向输入表单,所以我应该如何连接查询与该输入。那么此代码是否有效或需要进行任何修改?
你可以尝试这样的事情:
public static function getAll() {
$query = DB::table('radio_city');
if($city = Input::get('city')) {
$query->where('city', $city);
}
return $query->groupBy('city')->get();
}
阅读更多关于documentation的信息。另请参阅Eloquent ORM
和Scope。您应该首先阅读文档,一切都在那里。
最像是http://laravel.com/docs/queries#joins,因为Lavarel使用查询包装器,它不使用ORM,如果你使用ORM框架(例如Django)或PHP的ORM框架(例如,它在Symfony中使用的Doctrine http://www.doctrine-project.org/)你可以在没有SQL的情况下使查询更加高级,但是使用Laravel,你需要使用带有Laravel包装器的SQL。