希望区分$ query查询响应与其中的数据以及$ query查询响应

时间:2017-05-21 07:33:54

标签: php mysql laravel-5.4

我无法区分何时对其中的数据进行响应以及何时没有响应。我正在使用Laravel 5.4与连接的模型和控制器。我的代码和我的模型一样:

public function scopeCheckForPrevious($verify)

{
  json_encode($query = \App\Address::where('address', 'like', '%'.$verify.'%')

  ->get());

  return $query;



} 

这是我的控制器代码:

public function show()
    {
      $result = new Address;
      $check = $_POST['address'];

      $address = $result->scopeCheckForPrevious($check);

      print_r($address);

    }

当我打印结果时,当我有一个返回数据的$查询时,我就会这样做:

Illuminate \ Database \ Eloquent \ Collection Object([items:protected] => Array([0] => App \ Address Object([fillable:protected] =>数组([0] =>地址[1] => city [2] => state)[connection:protected] => mysql [table:protected] => [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array()[perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] =>数组( [id] => 1 [地址] => 1024 s主要st [city] => corona [state] => ca [created_at] => 2017-05-15 01:45:28 [updated_at] => 2017-05-15 01:45:28)[original:protected] =>数组([id] => 1 [地址] => 1024 s主要st [city] =>电晕[状态] ] => ca [created_at] => 2017-05-15 01:45:28 [updated_at] => 2017-05-15 01:45:28)[演员:受保护] =>数组()[ date:protected] => Array()[dateFormat:protected] => [appends:protected] => Array()[events:protected] =&g吨; Array()[observables:protected] => Array()[关系:受保护] => Array()[touches:protected] => Array()[timestamps] => 1 [hidden:protected] => Array()[visible:protected] => Array()[guarded:protected] =>数组([0] => *))[1] => App \ Address Object([fillable:protected] => Array([0] => address [1] => city [2] => state)[connection:protected] => mysql [table:protected ] => [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array()[perPage:protected] => 15 [ exists] => 1 [wasRecentlyCreated] => [attributes:protected] =>数组([id] => 4 [地址] => 1024 s主要st。[city] =>日冕[州] => ca [created_at] => 2017-05-15 01:47:39 [updated_at] => 2017-05-15 01:47:39)[original:protected] =>数组([id] => 4 [地址] => 1024 s主要st。[city] => corona [state] => ca [created_at] => 2017-05-15 01:47:39 [updated_at] =&gt ; 2017-05-15 01:47:39)[casts:protected] => Array()[dates:protected] => Array()[dateFormat:protected] => [appends:protected] => Array()[events:protected] => Array()[observables:protected] => Array()[relations:protected] => Array()[ touches:protected] => Array()[timestamps] => 1 [hidden:protected] => Array()[visible:protected] => Array()[guarded:protected] =>数组([0] => *))))

当我返回一个没有响应的查询时,我就有了这个:

Illuminate \ Database \ Eloquent \ Collection Object([items:protected] => Array())

我想制作一个if else语句并尝试检查$ address是否为NULL,如下所示:

public function show()
{
  $result = new Address;
  $check = $_POST['address'];

  $address = $result->scopeCheckForPrevious($check);



  if($address == NULL)
 {
    print_r($address);
  }

  else
  {
    echo "You have no responses";
  }

但它一直只响应else echo。

非常感谢任何有关此问题的帮助

1 个答案:

答案 0 :(得分:1)

尝试使用count()

if( count($address) == 0)
{
    echo "You have no responses";
}

else
{
    print_r($address);
}