下面的函数在我的create方法中工作,是我的另一个post的解决方案。我似乎无法在我的update方法中工作--一个Call to a member function storeAs() on string错误持续存在。错误屏幕突出显示foreach循环。
edit.php
public function updateTentry($id)
{
$tentry = Tentry::find($id);
$images = [
'image_plant_general',
'image_plant_closeup',
'image_fruit_in_plant',
'image_fruit_in_plant_closeup',
'image_fruit_in_harvest_single',
'image_fruit_in_harvest_group'
];
$data = [
'trial_id' => $this->trial->id,
'pt_plant_vigor' => $this->pt_plant_vigor,
'pt_plant_color' => $this->pt_plant_color,
'pt_plant_growth' => $this->pt_plant_growth,
...
// more variables
];
foreach ($images as $image) {
$data[$image] = $this->{$image}?->storeAs($this->evaluation->id, $this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $this->{$image}->getClientOriginalExtension(), 'trial-entry-photos');
}
$tentry->update($data);
session()->flash('success', 'Tentry Updated Successfully');
return redirect()->route('tentry.edit', [$this->evaluation->id, $this->trial->id, $tentry->id]);
}发布于 2022-06-05 07:23:46
存储和存储函数可在UploadedFile上使用。
$request->file()返回IlluminateHttp\UploadedFile的一个实例,其中存在store和storeAs()方法。
因此,您需要更改以下内容
foreach ($images as $image) {
$data[$image] = $this->{$image}?->storeAs($this->evaluation->id, $this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $this->{$image}->getClientOriginalExtension(), 'trial-entry-photos');
}至
foreach ($images as $image) {
if(request()->hasFile($image) && request()->file($image)->isValid()) {
$uploadedFile = request()->file($image);
$data[$image] = $uploadedFile->storeAs(
/** path where the uploaded file is to be stored */
$this->evaluation->id,
/** Filename by which the uploaded file must be stored */
$this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $uploadedFile->getClientOriginalExtension(),
/** name of one of the disks defined in the config/filesystems.php */
'trial-entry-photos');
}
}https://stackoverflow.com/questions/72505356
复制相似问题