Laravel检查是否有来自模型的数据 [英] Laravel Checking whether it has data or not from the model

查看:204
本文介绍了Laravel检查是否有来自模型的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am receiving the data from url and sending it to the model to and then displaying in the model like this.

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

然后在视图中.

但是要检查它是否有数据,我正在这样的视图中进行操作:

But to check whether it has the data or not I am doing that in the view like this:

<?php

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

但是两者都显示了数组.

But both of the it shows the array.

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

How can i check whether it given $data exist or not in the database.

推荐答案

您最有可能找回Illuminate/Database/Eloquent/Collection对象,并且 truthy 检查又恢复为true,因为该对象在那里.但是,您可以使用该对象中内置的isEmpty()方法.

You are most likely getting back an Illuminate/Database/Eloquent/Collection object and the truthy check is coming back as true since the object is there. What you can do though is use the isEmpty() method built into that object.

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

您认为:

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

这篇关于Laravel检查是否有来自模型的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆