2016-09-25 2 views
0

이 문제가 발생하는 데 문제가 있습니다. 기본적으로이 기능은 사용자 설정의 삭제 버튼을 클릭하면 호출됩니다. 사용자는 여러 구성을 가질 수 있습니다. 한 번 설정을 삭제하면 다른 설정을 찾으면 현재 설정으로 설정합니다. 그것은 0으로 다시 config_done를 설정해야하지 않으면 첫 번째 비트는 잘 작동하지만, 경우 $firstProfile 반환하는 대신 나에게 오류를 반환 else를 입력 아무것도 ..Laravel 5.3 결과가 존재하는지 확인

코드 :

public function delete(UserConfig $config) { 
    $user = Auth::user(); 
    $uid = $user->id; 

    if ($uid == $config->user_id) { 

     UserConfig::where('id', $config->id)->delete(); 

     $firstProfile = UserConfig::where('user_id', $uid)->first()->id; 

     if(count($firstProfile)){ 
     $user = User::find($uid); 
     $user->current_profile = $firstProfile; 
     $user->save(); 

     return view('/settings'); 
     } else { 
     $user = User::find($uid); 
     $user->config_done = 0; 
     $user->save(); 

     return view('/home'); 
     } 
    } 
    } 

오류 :

ErrorException in UserConfigController.php line 100: 
Trying to get property of non-object 
in UserConfigController.php line 100 
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/jordykoppen/git/beef-monitor/app/Http/Controllers/UserConfigController.php', '100', array('config' => object(UserConfig), 'user' => object(User), 'uid' => '1')) in UserConfigController.php line 100 

저는 Laravel 및 전체 MVC 환경에 매우 익숙합니다.

+0

Eloquent는'User' 결과를'if' 문으로 감쌀 수있는 결과가 없으면 null을 반환합니다. 컬렉션이라면 http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty – arma

+0

중 하나를 사용하여 확인할 수 있습니다. 예제에서'$ firstProfile'을'if'에 래핑하여 시도해 보았습니다 문을 반환하지만 오류를 반환합니다. '객체가 아닌 객체의 속성을 얻으려고합니다.' –

답변

1

Eloquent는 결과가없는 경우 null을 반환합니다. 여기서 개체를 다루므로 카운트를 확인하는 대신이 작업을 수행 할 수 있습니다.

$firstProfile = UserConfig::where('user_id', $uid)->first(); 

if($firstProfile){ 
    // success 
} else { 
    // not result 
} 

$firstProfileUserConfig 변수의 인스턴스가되거나 그 null 카운트를 체크 할 필요가 될 것이다.

+0

감사합니다. 그렇다면 당신은'first()'이후에'-> id'를 꺼냈다는 것을 알았습니다. 그래서 저는 똑같이했고 그 것이 문제를 해결했습니다! 다시 한번 감사드립니다. –