Laravel 9 和 Livewire 2 多个 storeAs 方法抛出 "Call to a member function storeAs() on null" 错误

Laravel 9 and Livewire 2 multiple storeAs methods throwing "Call to a member function storeAs() on null" error

我有 6 张不同的图片需要用户上传,所以我有 6 种不同的 storeAs 方法。当我只有一个时,一切正常,但有 2 个或更多时,我会收到 Call to a member function storeAs() on null 错误。老实说,我的代码看起来重复又脏,所以我并不感到惊讶。

public function createTentry($id)
{
    $trial = Trial::find($id);

    $image_plant_general = $this->image_plant_general->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_plant_general->getClientOriginalExtension(), 'trial-entry-photos');
    $image_plant_closeup = $this->image_plant_closeup->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_plant_closeup->getClientOriginalExtension(), 'trial-entry-photos');
    $image_fruit_in_plant = $this->image_fruit_in_plant->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_plant->getClientOriginalExtension(), 'trial-entry-photos');
    $image_fruit_in_plant_closeup = $this->image_fruit_in_plant_closeup->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_plant_closeup->getClientOriginalExtension(), 'trial-entry-photos');
    $image_fruit_in_harvest_single = $this->image_fruit_in_harvest_single->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_harvest_single->getClientOriginalExtension(), 'trial-entry-photos');
    $image_fruit_in_harvest_group = $this->image_fruit_in_harvest_group->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_harvest_group->getClientOriginalExtension(), 'trial-entry-photos');

    Tentry::create([
        ...
        'image_plant_general' => $image_plant_general,
        'image_plant_closeup' => $image_plant_closeup,
        'image_fruit_in_plant' => $image_fruit_in_plant,
        'image_fruit_in_plant_closeup' => $image_fruit_in_plant_closeup,
        'image_fruit_in_harvest_single' => $image_fruit_in_harvest_single,
        'image_fruit_in_harvest_group' => $image_fruit_in_harvest_group,

    ]);

    return redirect()->route('trial.show', [$trial->evaluation_id, $trial->id]);
}

我建议您使用数组和循环,而不是检查单个属性。使代码更清晰,并且更容易一遍又一遍地检查同一件事。

另一件事,我添加了 model-route-binding,这样 Trial 模型就可以直接作为参数注入,而无需显式查找。

public function createTentry(Trial $trial)
{
    $prefix = $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.';

    $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 = [
        // Other Tentry-fields here
    ];

    foreach ($images as $image) {
        $data[$image] = $this->{$image}
            ? $this->{$image}->storeAs('/', $prefix.$this->image_plant_general->getClientOriginalExtension(), 'trial-entry-photos')
            : null;
    }

    Tentry::create($data);

    return redirect()->route('trial.show', [$trial->evaluation_id, $trial->id]);
}