Home > Blockchain >  File update laravel 8 Undefined variable: filename and filepath
File update laravel 8 Undefined variable: filename and filepath

Time:04-21

I want to upload a blogger's profile image but I get an error:

Undefined variable: filename

Undefined variable: filepath

I think it is a controller issue. I don't know why the variable is not defined, though I declared the same variable name.

So please help me out.

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'
                ]);
        }
    }
}

CodePudding user response:

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->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'
            ]);
    }
}

}

  • Related