Laravel:更新数据时在 null 上调用成员函数 getClientOriginalName()
Posted
技术标签:
【中文标题】Laravel:更新数据时在 null 上调用成员函数 getClientOriginalName()【英文标题】:Laravel : Call to a member function getClientOriginalName() on null when Update Data 【发布时间】:2021-11-25 17:58:51 【问题描述】:我将更新其包含图像的数据,然后当我发送更新时出现错误调用成员函数 getClientOriginalName()
这是我的控制器
public function updateBook(Request $request, $id)
$book = DB::table('books')->where('id',$id);
$old = $book->select('images');
$ChecK = 0;
$file = var_dump($request->file('images')->getClientOriginalName());
$filename = time().$file;
$path = public_path('/Uploads');
if($request->hasFile('images'))
$file = $request->file('images');
$file->move($path, $filename);
Storage::delete($old);
$ChecK = 1;
else
dd('Request Hash No File!');
$data = [
'name'=> $request->input('bookName'),
'year'=> $request->input('tahunT'),
'author'=> $request->input('author'),
'summary'=> $request->input('Summary'),
'publisher'=> $request->input('publishers'),
'pageCount' => $request->input('pageCount'),
'readPage'=> $request->input('ReadPage'),
'finished' => '$pageCount' == '$readPage' ? true : false,
'reading' => '$readPage' > 0 ? true : false,
'updatedAt'=> new DateTime()
];
if( $ChecK == 1)
$data = ['images' => $filename];
$book->update($data);
return redirect('home')->with('status', 'update succesfully');
else
$data = ['images' => $old];
$book->update($data);
return redirect('home')->with('status', 'Update with old image ');
这是视图
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
session('status')
</div>
@endif
@foreach ($toEdit as $dt)
<form class="row g-3" enctype="multipart/form-data" method="POST" action=" route('update', $dt->id) " value="PUT">
csrf_field()
<div class="col-md-6">
<label for="bookName" class="form-label">Nama Buku:</label>
<input type="text" class="form-control" name ="bookName"id="bookName" value=" ($dt->name) ">
</div>
<div class="col-md-3">
<label for="tahunT" class="form-label">Tahun Terbit : </label>
<input type="number" class="form-control" name="tahunT" id="tahunT" value=" ($dt->year) ">
</div>
<div class="col-md-3">
<label for="author" class="form-label">Author : </label>
<input type="text" class="form-control" name="author" id="author" value=" ($dt->author) ">
</div>
<div class="col-md-6">
<label for="publishers" class="form-label">Publisher : </label>
<input type="text" class="form-control" name="publishers" id="publishers" value=" ($dt->publisher) ">
</div>
<div class="col-md-3">
<label for="pageCount" class="form-label">Page Count :</label>
<input type="number" class="form-control" name="pageCount" id="pageCount" value=" ($dt->pageCount) ">
</div>
<div class="col-md-3">
<label for="ReadPage" class="form-label">Read Page :</label>
<input type="number" class="form-control" name="ReadPage" id="ReadPage"value=" ($dt->readPage) ">
</div>
<div class="col-12">
<label for="Summary" class="form-label">Summary :</label>
<textarea class="form-control" name="Summary" id="Summary"> ($dt->summary) </textarea>
</div>
<div class="col-md-12">
<label for="images" class="form-label">Book Image :</label>
<input type="file" class="form-group" name="images">
<br>
<img src=" asset('Uploads/'.$dt->images) " widht="300px"/>
<br>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
@endforeach
</div>
在这里,我会更新数据,如果用户想要更改图像,那么图像将被更新,如果没有,图像使用旧图像。它与其他输入相同。但是,当我尝试上传新的时,错误 Call to a member function getClientOriginalName()
发生了。
你对此有什么建议吗?我已经添加了代码enctype="multipart/form-data"
和var_dump()
,但错误是一样的。谢谢你:)
【问题讨论】:
【参考方案1】:您必须通过条件检查您的image
是否为null,以避免出现此错误。
尝试类似:
if ( isset($request->file('images')) != null )
$ChecK = 0;
$file = $request->file('images')->getClientOriginalName();
$filename = time().$file;
$path = public_path('/Uploads');
//..
【讨论】:
【参考方案2】:只需添加一个简单的条件:
If($request->file('image'))
...
$request->images->getClientOriginalNe();
.....
【讨论】:
您好,这段代码运行良好,谢谢以上是关于Laravel:更新数据时在 null 上调用成员函数 getClientOriginalName()的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 8 的问题:在 null 上调用成员函数 count()
在 null sanctun laravel mongodb 上调用成员函数 prepare()
Laravel 护照抛出错误:“在 null 上调用成员函数 createToken()”