第一次使用lavavel数据库,在wenbamn admin中执行查询的方法,
select * from table where 1=1,
如何 在模型内 设置别名? from table as *** ,不用查询构造器或者直接写sql能否实现?
/**
* 执行查询
* @param array $where
* @param string|null $field
* @param string $order
* @return EloquentBuilder|QueryBuilder|Model
*/
protected function doSelect(array $where, string $field = null, string $order= 'desc'){
$model = $this->model;
foreach ($where as $column => $value) {
if (is_array($value)) {
if (in_array($value[0], ['>', '=', '<', '<>', 'like', 'not like'])) {
$model = $model->where($column, $value[0], $value[1]);
} elseif ($value[0] == 'in') {
$model = $model->whereIn($column, $value[1]);
} elseif ($value[0] == 'not in') {
$model = $model->whereNotIn($column, $value[1]);
} elseif ($value[0] == 'null') {
$model = $model->whereNull($column, $value[1]);
} elseif ($value[0] == 'not null') {
$model = $model->whereNotNull($column, $value[1]);
} else {
$model = $model->whereBetween($column, $value);
}
} else {
$model = $model->where($column, $value);
}
}
if ($field) {
$model = $model->orderBy($field, $order);
}
return $model;
}
除了下方这种,和 原生sql 如何在model内设置?
DB::table('users as table1')->select(DB::raw('table1.id'))->get();
小白一枚,谢谢解答!