我有以下从 laravel 获得的输出,我将它们作为选项插入到选择框中。但是对于lunch option
我最后的选择是空的。如何在laravel中返回非空的行?
{"id":18,"guest_origin":"Bulgaria","heard_where":"","staying_at":"","lunch_option":"Cheese"},{"id":19,"guest_origin":"Chech Republic","heard_where":"","staying_at":"","lunch_option":"Chicken"},{"id":20,"guest_origin":"China","heard_where":"","staying_at":"","lunch_option":"Ham"},{"id":21,"guest_origin":"Denmark","heard_where":"","staying_at":"","lunch_option":""},{"id":22,"guest_origin":"Finland","heard_where":"","staying_at":"","lunch_option":""},{"id":23,"guest_origin":"Israel","heard_where":"","staying_at":"","lunch_option":""},{"id":24,"guest_origin":"Malaysia","heard_where":"","staying_at":"","lunch_option":""},{"id":25,"guest_origin":"Norway","heard_where":"","staying_at":"","lunch_option":""},
Controller .php
function getComboselect( Request $request)
{
if($request->ajax() == true && \Auth::check() == true)
{
$param = explode(':',$request->input('filter'));
$parent = (!is_null($request->input('parent')) ? $request->input('parent') : null);
$limit = (!is_null($request->input('limit')) ? $request->input('limit') : null);
$rows = $this->model->getComboselect($param,$limit,$parent);
$items = array();
$fields = explode("|",$param[2]);
foreach($rows as $row)
{
$value = "";
foreach($fields as $item=>$val)
{
if($val != "") $value .= $row->{$val}." ";
}
$items[] = array($row->{$param['1']} , $value);
}
return json_encode($items);
}
模型.php
static function getComboselect( $params , $limit =null, $parent = null)
{
$limit = explode(':',$limit);
$parent = explode(':',$parent);
if(count($limit) >=3)
{
$table = $params[0];
$condition = $limit[0]." `".$limit[1]."` ".$limit[2]." ".$limit[3]." ";
if(count($parent)>=2 )
{
$row = \DB::table($table)->where($parent[0],$parent[1])->get();
$row = \DB::select( "SELECT * FROM ".$table." ".$condition ." AND ".$parent[0]." = '".$parent[1]."'");
} else {
$row = \DB::select( "SELECT * FROM ".$table." ".$condition);
}
}else{
$table = $params[0];
if(count($parent)>=2 )
{
$row = \DB::table($table)->where($parent[0],$parent[1])->get();
} else {
$row = \DB::table($table)->get();
}
}
return $row;
}
此代码正在使用 http://carlosdeoliveira.net/jcombo/?lang=en .如果您查看项目链接上的示例,您将看到它正在使用父(州)列出子(城市)以进行列表。我没有使用父级,所以没有任何东西分配给变量 $parent[0] 和 $parent[1],因此没有什么可担心的,但对于其余的,我将尝试在下面发布每个结果,这样你会有更好的主意.我的理解是model.php 正在使用
$row = \DB::table($table)->get();
将数据传递给controllers.php如果您查看屏幕截图,您会看到我有超过 1 列来列出选项。如果我写 $row = \DB::table($table)->whereRaw('lunch <> ""')->get();
,我就不能在那里写一个列名这将带来直到 Id 4 的选项。在这种情况下,Holland 不在 guest 来源的选项列表中。一旦model.php通过
$row
到controllers.php,它为每个变量返回以下结果。print_r($row);
stdClass Object ( [id] => 48 [guest_origin] => Other [heard_where] => [staying_at] => [lunch_option] => )
print_r($rows);
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [id] => 1 [guest_origin] => Western Australia [heard_where] => Wildsights Office [staying_at] => Wildsights Villas [lunch_option] => Chicken ) 1 => stdClass Object ( [id] => 2 [guest_origin] => Rest of Australia [heard_where] => Brochure [staying_at] => Wildsights Beach Units [lunch_option] => Cheese ) [2] => stdClass Object ( [id] => 3 [guest_origin] => Germany & Austria [heard_where] => Sign [staying_at] => Bay Lodge Backpackers [lunch_option] => Ham ) [3] => stdClass Object ( [id] => 4 [guest_origin] => UK & Eire [heard_where] => Word of Mouth [staying_at] => Blue Dolphin Caravan Park [lunch_option] => Tuna )
print_r($fields);
Array ( [0] => staying_at )
print_r($value);
什么都不打印
print_r($items);
[8] => Array ( [0] => Shark Bay Holiday Cottages 1 => Shark Bay Holiday Cottages ) [9] => Array ( [0] => Shark Bay Hotel 1 => Shark Bay Hotel )
希望很清楚,您可以帮助我在进入循环之前过滤空行。
最佳答案
最合适的方法是使用 whereRaw
运算符而不是 where
.
e.x 以下查询将获取除空 ("")
以外的所有数据字段列表中的值。
DB::table('mytable')->whereRaw('field <> ""')->get();
关于php - 返回 Laravel 中非空的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309905/