拉拉维尔 检查它是否有来自模型的数据


Laravel Checking whether it has data or not from the model

我从url接收数据并将其发送到模型,然后像这样显示在模型中。

public function BlogDisplay($data=NULL)
    {
        $BlogData = BlogModel::where('BlogLink', '=', $data)->get();
        return View::make('Blogs', array('BlogData' => $BlogData));

然后在视图中。

但是为了检查它是否有数据,我在这样的视图中这样做:

<?php
   if($BlogData)
   {
     var_dump($BlogData);
     echo 'has data';
   }
   else
   {
     var_dump($BlogData);
     echo 'has no data';
   }
?>

但它都显示了数组。

如何检查数据库中是否存在给定$data。

您很可能正在返回一个Illuminate/Database/Eloquent/Collection对象,并且由于对象在那里,真实检查返回为真。不过,您可以做的是使用该对象中内置的isEmpty()方法。

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_isEmpty

在您看来:

@if ($BlogData->isEmpty())
    Nothing Here
@else 
    Got data!
@endif