返回laravel中非空的行

Joh*_*nny 3 php select json laravel

我有以下输出,我从laravel得到,我将它们插入选择框作为选项.但是对于lunch option我的最后选择是空的.如何在laravel中返回非空的行?

{ "ID":18, "guest_origin": "保加利亚", "heard_where": "", "staying_at": "", "lunch_option": "干酪"},{ "ID":19, "guest_origin":" Chech Republic","hear_where":"","stay_at":"","lunch_option":"Chicken"},{"id":20,"guest_origin":"China","hear_where":""," staying_at ":"", "lunch_option": "火腿"},{ "ID":21, "guest_origin": "丹麦", "heard_where": "", "staying_at": "", "lunch_option": "" },{ "ID":22, "guest_origin": "芬兰", "heard_where": "", "staying_at": "", "lunch_option": ""},{ "ID":23, "guest_origin": "以色列", "heard_where": "", "staying_at": "", "lunch_option": ""},{ "ID":24, "guest_origin": "马来西亚", "heard_where": "","staying_at ":"", "lunch_option": ""},{ "ID":25, "guest_origin": "挪威", "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);     
    } 
Run Code Online (Sandbox Code Playgroud)

Model.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;
}
Run Code Online (Sandbox Code Playgroud)

此代码使用http://carlosdeoliveira.net/jcombo/?lang=en.如果您查看项目链接上的示例,您将看到它正在使用父(州)列出列表的子(城市).我没有使用父级,所以没有任何东西被绑定到变量$ parent [0]和$ parent [1],因此没有什么可担心的,但对于其余的,我会尝试发布下面的每个结果,所以,你会有一个更好的主意.我的理解是model.php正在将数据传递给controllers.php使用. $row = \DB::table($table)->get();如果您查看屏幕截图,您会看到我有超过1列列出选项.我不能在那里写一个列名,如果我写$row = \DB::table($table)->whereRaw('lunch <> ""')->get();这个带来选项直到Id 4.在这种情况下,Holland不在客人来源的选项列表中.

一旦model.php传递$row给controllers.php,它就会为每个变量返回以下结果.

print_r($row);
Run Code Online (Sandbox Code Playgroud)

stdClass对象([id] => 48 [guest_origin] =>其他[hear_where] => [stay_at] => [lunch_option] =>)

print_r($rows);
Run Code Online (Sandbox Code Playgroud)

Illuminate\Support\Collection Object([items:protected] => Array([0] => stdClass Object([id] => 1 [guest_origin] =>西澳大利亚州[hear_where] => Wildsights Office [stay_at] => Wildsights别墅[lunch_option] =>鸡)1 => stdClass对象([id] => 2 [guest_origin] =>澳大利亚其他地区[hear_where] =>宣传册[stay_at] => Wildsights Beach Units [lunch_option] => Cheese)[ 2] => stdClass对象([id] => 3 [guest_origin] =>德国和奥地利[hear_where] =>签署[stay_at] => Bay Lodge Backpackers [lunch_option] => Ham)[3] => stdClass对象( [id] => 4 [guest_origin] =>英国和爱尔兰[hear_where] =>口碑[stay_at] => Blue Dolphin Caravan Park [lunch_option] =>金枪鱼)

print_r($fields);
Run Code Online (Sandbox Code Playgroud)

数组([0] => stay_at)

print_r($value); 
Run Code Online (Sandbox Code Playgroud)

没有打印

print_r($items);
Run Code Online (Sandbox Code Playgroud)

[8] =>数组([0] =>鲨鱼湾度假小屋1 =>鲨鱼湾度假小屋)[9] =>数组([0] =>鲨鱼湾酒店1 =>鲨鱼湾酒店)

希望它很清楚,你可以帮助我在进入循环之前过滤空行.

小智 15

您可以使用宏,它会检查字段是否为非空和非空字符串。只需将这些代码行添加到您的 AppServiceProvider 中

use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

public function boot()
{
    QueryBuilder::macro(
        'whereNotEmpty',
        function (string $column) {
            return $this->whereNotNull($column)
                ->where($column, '<>', '');
        }
    );

    EloquentBuilder::macro(
        'whereNotEmpty',
        function (string $column) {
            return $this->getQuery()
                ->whereNotEmpty($column);
        }
    );
}
Run Code Online (Sandbox Code Playgroud)

现在你允许这样使用它:

$posts = Post::whereNotEmpty('field')->get();
Run Code Online (Sandbox Code Playgroud)


Cle*_*ode 12

最合适的方法是使用whereRaw运算符而不是where.

ex以下查询将获取("")字段列表中除空值以外的所有数据.

DB::table('mytable')->whereRaw('field <> ""')->get();
Run Code Online (Sandbox Code Playgroud)