Support the ongoing development of Laravel.io →
Views Database Laravel

At the moment I am trying to upload a file via CMS and that bit works absolutely fine. But I need to include a title and description along with it. When I Upload a file, it doesn't add the title and description to the database. I'm not sure why it isn't adding to the database and it is appearing with this error:

array_flip() expects parameter 1 to be array, string given.

Here is the controller file with the function explained:

        

     public function postUpload(Request $request)
    {

        $this->validate($request, [
            'file' => 'required|mimes:' . implode(',', $this->allowedUploadTypes) . '',
            'title' => 'required',
            'description' => 'required',
        ]);

        if ($request->hasFile('file')) {
            $uploadedFile = $request->file('file');

            if ($uploadedFile->isValid()) {

                // check if path exists
                $path = storage_path('app/' . $request->get('path'));

                if (!is_dir($path)) {
                    $error = 'Storage path doesn\'t exist';
                } else {
                    // check if file exists
                    $newFile = $this->getFileName($path, $uploadedFile->getClientOriginalName());

                    // store file


                    if (!$request->file('file')->storeAs(
                        $request->get('path'), $newFile
                    )) {
                        dd('nope');
                    }

                    $input = $this->handleInput($request->all());

                    $fileManager= new FileManagerFiles();
                    $fileManager->file_id;
                    $fileManager->title = (isset($input['title']) && !empty($input['title'])) ? $input['title'] : 'image';
                    $fileManager->description = $input['description'];

                    $fileManager->save();
                    // redirect
                    return redirect()->back()->with('success', 'File uploaded: ' . $newFile);
                }
            }
			else {
				$error = 'The file was not valid';
			}
		}
		else {
			$error = 'Unknown error';
		}

		return redirect()->back()->with('failure', $error);
	}

I feel like it is only a small error as it uploads the file fine with the storage feature. If there is a solution, if possible I need the whole file storage code to remain as that is important for the CMS. I just need the title and description to go to the database successfully.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Luke Frost Luke Frost lukefrost Joined 9 Aug 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.