saly3301
1 week ago

Need help to store data

Posted 1 week ago by saly3301

Hi,

I have a problem when I want to save a new data I don't want to upload file too but I have to code multiple uploads in my public function store. I want to add file when I open edit form and uploaded. what should I do?

I have tried to set validation to set but when I submit error Undefined variable: data.

public function store(Request $request)
{
    $request->validate([
        'employee_sid' => 'required',
        'employee_npk' => 'required',
        'employee_name' => 'required',
        'file_uploads' => 'nullable',
        'file_uploads.*' => 'file|image|mimes:pdf,jpg,jpeg,png',
    ]);

    if($request->file('file_uploads'))
    {
        foreach($request->file('file_uploads') as $file)
        {
            $name = $file->getClientOriginalName();
            $path = 'public/file/'.$request->employee_name;
            $file->move($path, $name);
            $data[] = $name;
        }
    }

    $sid = new Sid;
        $sid->employee_sid          = $request->employee_sid;
        $sid->employee_npk          = $request->employee_npk;
        $sid->employee_name         = $request->employee_name;
        $sid->file_uploads          = json_encode($data);
        $sid->save();

I expect when I submit that data without the file_uploads it will. but error

Undefined variable: data.

Please sign in or create an account to participate in this conversation.