2017-05-21 38 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