在laravel中返回非空的行

时间:2017-04-09 17:45:22

标签: php json laravel select

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

  

{ “ID”:18, “guest_origin”: “保加利亚”, “heard_where”: “”, “staying_at”: “”, “lunch_option”: “干酪”},{ “ID”:19,“guest_origin “:”再检查一下   共和国 “ ”heard_where“: ”“, ”staying_at“: ”“, ”lunch_option“: ”鸡“},{ ”ID“:20, ”guest_origin“: ”中国“, ”heard_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”: “”},< / p>

enter image description here

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);     
    } 

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;
}

此代码正在使用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不在客户来源的选项列表中。

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

print_r($row);
  

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

print_r($rows);
  

Illuminate \ Support \ Collection Object([items:protected] =&gt;数组(   [0] =&gt; stdClass对象([id] =&gt; 1 [guest_origin] =&gt;西澳大利亚州   [hear_where] =&gt; Wildsights Office [stay_at] =&gt; Wildsights别墅   [lunch_option] =&gt;鸡)1 =&gt; stdClass对象([id] =&gt; 2   [guest_origin] =&gt;澳大利亚其他地区[hear_where] =&gt;小册   [stay_at] =&gt; Wildsights Beach Units [lunch_option] =&gt;奶酪)[2]   =&GT; stdClass对象([id] =&gt; 3 [guest_origin] =&gt;德国&amp;奥地利[hear_where] =&gt;签署[stay_at] =&gt; Bay Lodge Backpackers   [lunch_option] =&gt;火腿)[3] =&gt; stdClass对象([id] =&gt; 4   [guest_origin] =&gt;英国&amp; Eire [hear_where] =&gt;口口相传   [stay_at] =&gt; Blue Dolphin Caravan Park [lunch_option] =&gt;金枪鱼)

print_r($fields);
  

数组([0] =&gt; stay_at)

print_r($value); 

什么都不打印

print_r($items);
  

[8] =&gt;数组([0] =&gt;鲨鱼湾度假别墅1 =&gt;鲨鱼湾   假日小屋)[9] =&gt;数组([0] =&gt;鲨鱼湾酒店1 =&gt;鲨鱼   海湾酒店)

希望很清楚,你可以帮我在空行进入循环之前对其进行过滤。

2 个答案:

答案 0 :(得分:11)

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

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

DB::table('mytable')->whereRaw('field <> ""')->get();

答案 1 :(得分:5)

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

int cols = 5, rows = 5;

class Node
{
public:
    Node()
    {
        this->x = 0;
        this->y = 0;
    }

    Node(int i, int j)
    {
        this->x = i;
        this->y = j;
    }

    void AddNeighbors(Node** grid)
    {
        if (this->x < cols - 1)
        {
            this->neighbors.push_back(grid[this->x + 1][this->y]);
        }
        if (this->x > 0)
        {
            this->neighbors.push_back(grid[this->x - 1][this->y]);
        }
        if (this->y < rows - 1)
        {
            this->neighbors.push_back(grid[this->x][this->y + 1]);
        }
        if (this->y > 0)
        {
            this->neighbors.push_back(grid[this->x][this->y - 1]);
        }
    }

    int x;
    int y;
    vector<Node> neighbors;

    bool operator == (Node n2) 
    {
        return this->x == n2.x && this->y == n2.y;
    }

    bool operator != (Node n2) 
    {
        return this->x != n2.x && this->y != n2.y;
    }
};

void RemoveFromVector(vector<Node> &nodesSet, Node element)
{
    vector<Node>::iterator it = nodesSet.begin();

    for (int i = nodesSet.size() - 1; i >= 0; i--)
    {
        if (nodesSet[i] == element)
        {
            advance(it, i);
            nodesSet.erase(it);
            break;
        }
    }
}

bool ExistsInVector(vector<Node>& nodesSet, Node element)
{
    for (int i = 0; i < nodesSet.size(); i++)
    {
        if (nodesSet[i] == element)
        {
            return true;
        }
    }

    return false;
}

int main()
{
    Node** grid;

    grid = new Node* [cols];

    for (int i = 0; i < cols; i++)
    {
        grid[i] = new Node[rows];
        for (int j = 0; j < rows; j++)
        {
            Node node = Node(i, j);
            grid[i][j] = node;
        }
    }

    for (int i = 0; i < cols; i++)
    {
        for (int j = 0; j < rows; j++)
        {
            grid[i][j].AddNeighbors(grid);
        }
    }
}

现在您可以这样使用了:

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);
        }
    );
}