2017-05-21 40 views
0

我试图区分何时有数据响应和响应没有响应时出现问题。我正在使用连接了模型和控制器的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] => 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秒主st [city] => corona [state] => ca [created_at] => 2017-05-15 01:45:28 [updated_at] => 2017-05-15 01:45:28)[original :] []保护] =>阵列([id] => 1 [地址] => 1024秒主st [city] => corona [state] => ca [created_at] => 2017-05-15 01:45:28 [ Array()[dateFormat:protected] => [appends:protected] => Array()[] ()[events:protected] => Array()[observables:protected] => Array()[relations:protected] => Array()[touches:protected] => Array()[timestamps] => 1 [hidden :[]保护] => 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] => [primaryKe y: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 [地址] => 1024秒主城市[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] = > Array()[visible:protected] => Array()[guarded:protected] => Array([0] => *))))

当我返回没有响应的查询时,

Illuminate \ Database \ Eloquent \ C ollection对象([项目:保护] =>阵列())

我想打一个if else语句,并尝试只检查是否$地址为空或不喜欢这样的:

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"; 
    } 

但它只对其他回声做出反应。

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

回答

1

尝试使用count()

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

else 
{ 
    print_r($address); 
} 
0

洋洋洒洒返回一个集合,它总是会,你需要在这种情况下,做的是检查空集合。所以

if ($result->isEmpty()) { } 
if ($result->count() == 0) { } 
if (count($result) == 0) { } 

都会做同样的事情

+0

我得到的结果是唯一的收藏我从来没有回声出我想要的时候没有返回响应的声明。这并没有解决我的问题。 – watkins1179