이 문제가 발생하는 데 문제가 있습니다. 기본적으로이 기능은 사용자 설정의 삭제 버튼을 클릭하면 호출됩니다. 사용자는 여러 구성을 가질 수 있습니다. 한 번 설정을 삭제하면 다른 설정을 찾으면 현재 설정으로 설정합니다. 그것은 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 환경에 매우 익숙합니다.
Eloquent는'User' 결과를'if' 문으로 감쌀 수있는 결과가 없으면 null을 반환합니다. 컬렉션이라면 http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty – arma
중 하나를 사용하여 확인할 수 있습니다. 예제에서'$ firstProfile'을'if'에 래핑하여 시도해 보았습니다 문을 반환하지만 오류를 반환합니다. '객체가 아닌 객체의 속성을 얻으려고합니다.' –