이 이상한 문제가 있습니다.
블레이드 뷰에이 코드가 있습니다.
<input type="text" class="form-control" id="c_{{App\Models\Field::where([['material_id',$material->id],['question',$fillable->name]])->first()->id}}">
이 오류를 반환합니다.
Trying to get property 'id' of non-object (View: create.blade.php) (View:create.blade.php)
근데 이렇게 덤핑 dd()
하면
@php
dd(App\Models\Field::where([['material_id',$material->id],['question',$fillable->name]])->first()->id)
@endphp
그것은 나에게 오류를 보여주지 않고 레코드의 덤프를 id
여기서 뭐가 잘못 됐나요? 왜 한 곳에서는 작동하고 다른 곳에서는 작동하지 않습니까?
예, 따라서이 쿼리는 개체를 찾을 수없는 경우 오류를 생성 할 수 있습니다.
이 입력을 for 루프에서 사용하고 있다고 확신합니다. 그래서 당신이 dd 때 그것은 당신에게 첫 번째 객체를 얻을 것이고 그것이 존재하고 올바르게 렌더링 될 것입니다. dd를 제거하면 렌더링에 따라 쿼리를 여러 번 수행하고 잘못된 개체를 가져 오면 모든 스크립트가 중지됩니다.
대신 다음을 수행 할 수 있습니다.
@php
$field = App\Models\Field::where([['material_id',$material->id],['question',$fillable->name]])->first();
@endphp
@if($field && $field->id)
<input type="text" class="form-control" id="c_{{ $field->id}}">
@else
// warning field object not found
@endif
또는 @를 추가하여 오류를 억제 할 수 있습니다.
<input type="text" class="form-control" id="c_{{@ App\Models\Field::where([['material_id',$material->id],['question',$fillable->name]])->first()->id}}">
이와 같이 쿼리를 실행하면 n + 1 쿼리 문제가 발생할 수 있습니다. 즉, 쿼리를 여러 번 실행하게되며 이는 성능을위한 효율적인 방법이 아닙니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다