如何忽略laravel中的空数组集合

时间:2016-01-18 07:39:17

标签: php arrays collections laravel-5

您好我正在尝试从laravel 5.1中的关系中获取相关数据,它返回一个空数组集合。如何在laravel中忽略我的变量中的空数组集合。

 $brandsId = Input::has('brands') ? Input::get('brands') : null;
   $brands = Brand::findMany($brandsId);
   foreach($brands as $brand){
        $products = $brand->products()->paginate(15)//this will return an empty array if it doesnt find aything;
         echo "<pre>";
         print_r( $products);

空阵列碰撞看起来像这样。

Illuminate\Pagination\LengthAwarePaginator Object
(
    [total:protected] => 0
    [lastPage:protected] => 0
    [items:protected] => Illuminate\Database\Eloquent\Collection Object
        (
            [items:protected] => Array
                (
                )

        )

    [perPage:protected] => 15
    [currentPage:protected] => 1
    [path:protected] => http://localhost:8000/filter
    [query:protected] => Array
        (
        )

    [fragment:protected] => 
    [pageName:protected] => page
)

1 个答案:

答案 0 :(得分:1)

看看isEmpty()

  

如果集合为空,则isEmpty方法返回true;除此以外,   返回false:

if ($products->isEmpty()) echo "collection is empty"

在收集文档中,您将see all available functions