tp框架可以把查询条件在查询的外面组装起来,比如
$str = 'liling';
$map = [
'status' => 1,
'age' => ['>', 18],
'type' => ['in', [1,2,3]]
'name' => ['like', '%' . $str . '%']
];
model("User")->where($map)->select();
想知道laravel 这种 ">" , "in", "like"在外部怎么组装?
tp框架可以把查询条件在查询的外面组装起来,比如
$str = 'liling';
$map = [
'status' => 1,
'age' => ['>', 18],
'type' => ['in', [1,2,3]]
'name' => ['like', '%' . $str . '%']
];
model("User")->where($map)->select();
想知道laravel 这种 ">" , "in", "like"在外部怎么组装?
拼接方式有些不太一样, Laravel 是这样来的,多维数组,每个数组的参数顺序和直接使用 where 一样。
$where = [
['name', '=', 'user'],
];
Model::where($where)->first();
具体的实现在 Builder::addArrayOfWheres。
当传入的多维数组,键名是数字时,并且值是数组时,就平整的传入到 where 方法,否则就按照 where(..., =, ...)
的方式。
protected function addArrayOfWheres($column, $boolean, $method = 'where')
{
return $this->whereNested(function ($query) use ($column, $method, $boolean) {
foreach ($column as $key => $value) {
if (is_numeric($key) && is_array($value)) {
$query->{$method}(...array_values($value));
} else {
$query->$method($key, '=', $value, $boolean);
}
}
}, $boolean);
}
1 回答4.1k 阅读✓ 已解决
3 回答1.9k 阅读✓ 已解决
2 回答2.2k 阅读✓ 已解决
1 回答1.4k 阅读✓ 已解决
2 回答2.2k 阅读
1 回答610 阅读✓ 已解决
799 阅读
laravel 更面向对象一些