在 Laravel 4 中定义新查询
Defining new Query in Laravel 4
我目前正在使用 Laravel 4 为过滤器编写后端。该视图有一个包含四列的 table:id、area、subarea 和 城市。分别有四个文本输入,将用户的输入发送到后端。比如说,用户在 area 中输入 "AMP" 并在 city 中输入 "KUAL",后端应该 select通过 LIKE 语句对应模型。
我的想法是检查每个输入是否存在并链
->where()
约束。像这样:
public function handleAreasFilter() {
$data = Input::all();
if ($data['id'] == "" and $data['subarea'] == "" and $data['area'] == "" and $data['city'] == "") {
// If no filters are provided, we just return 20 subareas.
// Since subarea, area and city are tied with Eloquent Relationships
// (subarea belongs to an area, area has many subareas and belongs
// to a city, city has many areas) and we return the basis model
// (subarea), we add area and city names via a foreach loop.
$subs = Subarea::take(20)->get();
foreach ($subs as $s) {
$s->area = $s->area()->first()->name;
$s->city = $s->area()->first()->city()->first()->name;
}
return Response::json(array('status' => 'success', 'data' => $subs));
} else {
if ($data['id'] != "") {
$query = Subarea::whereId($data['id']);
}
if ($data['subarea'] != "") {
$query = $query->where('name', 'LIKE', '%' . $data['subarea'] . '%');
}
if ($data['area'] != "") {
$areas = Area::where('name', 'LIKE', '%' . $data['area'] . '%')->get();
// Here I try to distinguish between the first constraint
// and all other, to get
// $query->whereAreaId(..)->orWhereAreaId(..)->orWhere...
for ($i = 0; $i < $areas->count(); $i++) {
if ($i == 0) {
$query = $query->whereAreaId($areas[$i]->id);
} else {
$query = $query->orWhereAreaId($areas[$i]->id);
}
}
}
$subs = $query->get();
foreach ($subs as $s) {
$s->area = $s->area()->first()->name;
$s->city = $s->area()->first()->city()->first()->name;
}
return Response::json(array('status' => 'success', 'data' => $subs));
}
}
好吧,还没有完成,因为我偶然发现了以下问题:我不知道如何创建一个空查询。如果
$data['id'] == '',
那么$query遇到
就是undefined
$query = $query->where...
我知道我必须做类似的事情
$query = (isset($query)) ? $query : Subarea::newQuery();
但我显然试过了,但没有用。而且,
Subarea::all();
自然是这里的错误解决方案。好吧,我希望阅读的内容不会太多。请帮助:)
您可以使用 Subarea::query()
来 "start" 新查询。然后有条件地添加 where
s 。一个简单的例子:
$query = Subarea::query();
if($data['id'] !== ''){
$query->where('id', $data['id']);
}
$result = $query->get();
我目前正在使用 Laravel 4 为过滤器编写后端。该视图有一个包含四列的 table:id、area、subarea 和 城市。分别有四个文本输入,将用户的输入发送到后端。比如说,用户在 area 中输入 "AMP" 并在 city 中输入 "KUAL",后端应该 select通过 LIKE 语句对应模型。
我的想法是检查每个输入是否存在并链
->where()
约束。像这样:
public function handleAreasFilter() {
$data = Input::all();
if ($data['id'] == "" and $data['subarea'] == "" and $data['area'] == "" and $data['city'] == "") {
// If no filters are provided, we just return 20 subareas.
// Since subarea, area and city are tied with Eloquent Relationships
// (subarea belongs to an area, area has many subareas and belongs
// to a city, city has many areas) and we return the basis model
// (subarea), we add area and city names via a foreach loop.
$subs = Subarea::take(20)->get();
foreach ($subs as $s) {
$s->area = $s->area()->first()->name;
$s->city = $s->area()->first()->city()->first()->name;
}
return Response::json(array('status' => 'success', 'data' => $subs));
} else {
if ($data['id'] != "") {
$query = Subarea::whereId($data['id']);
}
if ($data['subarea'] != "") {
$query = $query->where('name', 'LIKE', '%' . $data['subarea'] . '%');
}
if ($data['area'] != "") {
$areas = Area::where('name', 'LIKE', '%' . $data['area'] . '%')->get();
// Here I try to distinguish between the first constraint
// and all other, to get
// $query->whereAreaId(..)->orWhereAreaId(..)->orWhere...
for ($i = 0; $i < $areas->count(); $i++) {
if ($i == 0) {
$query = $query->whereAreaId($areas[$i]->id);
} else {
$query = $query->orWhereAreaId($areas[$i]->id);
}
}
}
$subs = $query->get();
foreach ($subs as $s) {
$s->area = $s->area()->first()->name;
$s->city = $s->area()->first()->city()->first()->name;
}
return Response::json(array('status' => 'success', 'data' => $subs));
}
}
好吧,还没有完成,因为我偶然发现了以下问题:我不知道如何创建一个空查询。如果
$data['id'] == '',
那么$query遇到
就是undefined$query = $query->where...
我知道我必须做类似的事情
$query = (isset($query)) ? $query : Subarea::newQuery();
但我显然试过了,但没有用。而且,
Subarea::all();
自然是这里的错误解决方案。好吧,我希望阅读的内容不会太多。请帮助:)
您可以使用 Subarea::query()
来 "start" 新查询。然后有条件地添加 where
s 。一个简单的例子:
$query = Subarea::query();
if($data['id'] !== ''){
$query->where('id', $data['id']);
}
$result = $query->get();