【发布时间】:2021-08-17 12:29:56
【问题描述】:
我在更新我的表格数据时所有尝试都遇到了这个错误。
ErrorException: 从空值创建默认对象
AdminController.php
public function update(Request $r, $post_id) {
$post = Post::find($post_id);
$post->post_title = $r->post_title;
$post->post_image = $r->post_image;
$post->post_details = $r->post_details;
$post->post_rating = $r->post_rating;
$post->id = $r->id;
$post->save();
return back();
}
我的资源路线
Route::resource('post', AdminController::class);
刀片文件
<div class="edit-post-content">
<form action="{{ route('post.update',$list->post_id) }}" method="POST">
@csrf
@method('PUT')
<input type="hidden" name="id" value="{{$list->id}}">
<input type="hidden" name="post_rating" value="{{$list->post_rating}}">
<div class="mb-3">
Edit Title: <input class="form-control" name="post_title" type="text" id="exampleFormControlInput1" value="{{$list->post_title}}" aria-label="default input example">
</div>
<div class="mb-3">
Edit Description:
<textarea class="form-control" id="exampleFormControlTextarea1" name="post_details" rows="3">
{{ $list->post_details }}
</textarea>
</div>
<div>
<img src="{{asset('images/'.trim($list->post_image))}}" alt="" width="120px;">
Edit Photos:
<input id="formFileMultiple" class="form-control form-control-lg" name="post_image" type="file" value="{{ $list->post_image }}" multiple>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
有人可以帮帮我吗?
【问题讨论】:
-
Welcome to SO ...
find可以返回null,您需要在使用从find返回的内容作为对象之前检查这一点