文件更新 laravel 8 未定义的变量:文件名和文件路径

File update laravel 8 Undefined variable: filename and filepath

我想上传博主的个人资料图片,但出现错误:

Undefined variable: filename

Undefined variable: filepath

我认为是控制器的问题。虽然我声明了相同的变量名,但我不知道为什么没有定义变量。

所以请帮帮我。

public function update(Request $request)
{

    $this->validate($request, [
        'id' => 'required',
        'company' => 'required',
        'period' => 'required',
        'desc' => 'required',
        // 'file' => 'required|mimes:txt,xlx,xls,pdf,jpg,png|max:6048',
    ]);

    $id = $request->id;
    $send = Setting_product::findOrFail($id);
    if($request->file()) {
        $fileName = time().'_'.$request->file->getClientOriginalName();
        $filePath = $request->file('file')->storeAs('files/'.$request->company.'/TC', $fileName,  'public');
        $send->update([
            'company' => $request->company,
            'period' => $request->period,
            'desc' => $request->desc,
            'term_condition_file_path' => $filePath,
            'term_condition_file' => $fileName,
        ]);

        if ($send) {
            return redirect()
                ->route('setting_produk.edit', $id)
                ->with([
                    'success' => 'New send has been created successfully'
                ]);
        } else {
            return redirect()
                ->back()
                ->withInput()
                ->with([
                    'error' => 'Some problem occurred, please try again'
                ]);
        }
    }else{
        $send->update([
            'company' => $request->company,
            'period' => $request->period,
            'desc' => $request->desc
        ]);

        if ($send) {
            return redirect()
                ->route('setting_produk.edit', $id)
                ->with([
                    'success' => 'New send has been created successfully'
                ]);
        } else {
            return redirect()
                ->back()
                ->withInput()
                ->with([
                    'error' => 'Some problem occurred, please try again'
                ]);
        }
    }
}

public函数更新(请求$request) {

$this->validate($request, [
    'id' => 'required',
    'company' => 'required',
    'period' => 'required',
    'desc' => 'required',
    'file' => 'required|mimes:txt,xlx,xls,pdf,jpg,png|max:6048',
]);

$id = $request->id;
$send = Setting_product::findOrFail($id);
if ($request->hasFile('file')) {
    $fileName = time().'_'.$request->file->getClientOriginalName();
    $filePath = $request->file('file')->storeAs('files/'.$request->company.'/TC', $fileName,  'public');
    $send->update([
        'company' => $request->company,
        'period' => $request->period,
        'desc' => $request->desc,
        'term_condition_file_path' => $filePath,
        'term_condition_file' => $fileName,
    ]);

    if ($send) {
        return redirect()
            ->route('setting_produk.edit', $id)
            ->with([
                'success' => 'New send has been created successfully'
            ]);
    } else {
        return redirect()
            ->back()
            ->withInput()
            ->with([
                'error' => 'Some problem occurred, please try again'
            ]);
    }
}else{
    $send->update([
        'company' => $request->company,
        'period' => $request->period,
        'desc' => $request->desc
    ]);

    if ($send) {
        return redirect()
            ->route('setting_produk.edit', $id)
            ->with([
                'success' => 'New send has been created successfully'
            ]);
    } else {
        return redirect()
            ->back()
            ->withInput()
            ->with([
                'error' => 'Some problem occurred, please try again'
            ]);
    }
}

}