【发布时间】:2016-01-18 07:39:17
【问题描述】:
您好,我正在尝试从 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
)
【问题讨论】:
-
您可以随时使用
count($product)函数检查它是否为空。除此之外,我无能为力 - 如果我假设您正在尝试在视图中渲染它(或将非空结果放入其他内容)所有非空结果,那么您必须检查它是空的还是不是手动的。
标签: php arrays collections laravel-5