首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 9在图像上传时调用字符串上的成员函数storeAs()

Laravel 9在图像上传时调用字符串上的成员函数storeAs()
EN

Stack Overflow用户
提问于 2022-06-05 06:55:25
回答 1查看 99关注 0票数 0

下面的函数在我的create方法中工作,是我的另一个post的解决方案。我似乎无法在我的update方法中工作--一个Call to a member function storeAs() on string错误持续存在。错误屏幕突出显示foreach循环。

edit.php

代码语言:javascript
复制
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]);
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-05 07:23:46

存储存储函数可在UploadedFile上使用。

$request->file()返回IlluminateHttp\UploadedFile的一个实例,其中存在storestoreAs()方法。

因此,您需要更改以下内容

代码语言:javascript
复制
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');
    }

代码语言:javascript
复制
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');
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72505356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档