【问题标题】:Looking to distinguish between a $query response with data in it and a $query response without data in it希望区分包含数据的 $query 响应和不包含数据的 $query 响应
【发布时间】:2017-10-21 00:23:50
【问题描述】:

我在尝试区分何时有包含数据的响应以及何时没有响应时遇到了麻烦。我正在将 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);

    }

当我打印我的结果时,我有一个返回数据的 $query:

Illuminate\Database\Eloquent\Collection Object ([items:protected] => Array ([0] => 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] => Array ( [id] => 1 [address] => 1024 s main 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 [address] => 1024 s main st [city] => corona [state] => ca [created_at] => 2017-05-15 01:45:28 [updated_at] => 2017- 05-15 01:45:28 ) [casts:protected] => Array () [dates:protected] => Array () [dateFormat:protected] => [appends:protected] => Array () [events:protected ] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protecte d] => Array () [timestamps] => 1 [hidden:protected] => Array () [visible:protected] => Array () [guarded:protected] => Array ([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] => Array ([id] => 4 [address] => 1024 s main st. [city] => corona [state] => ca [created_at] => 2017-05-15 01:47:39 [updated_at] => 2017-05-15 01:47:39 ) [original:protected] =>数组([id] => 4 [address] => 1024 s main st. [city] => corona [state] => ca [created_at] => 2017-05-15 01:47:39 [updated_at] => 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] = > 数组 ( ) [可见:受保护] => 数组 () [受保护:受保护] => 数组 ( [0] => * ) ) ) )

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

Illuminate\Database\Eloquent\Collection 对象 ([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 回显来响应。

对于这个问题的任何帮助将不胜感激

【问题讨论】:

    标签: php mysql laravel-5.4


    【解决方案1】:

    尝试使用count()

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2012-04-03
      相关资源
      最近更新 更多