简体   繁体   中英

Failed to save file name in database in laravel

this is my addData method in UploadController

    public function addData(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|min:1',
            'email' => 'required|min:2',
        ]);

        if(Input::hasFile('file'))
        {
            $file = array('file' => Input::file('file'));
            $rules = array('file' => 'required|mimes:jpg,png,jpeg');
            $validator = Validator::make($file, $rules);

            if ($validator->fails()) {
                echo 'Not allowed!!';
            }
            else
            {
                if (Input::file('file')->isValid()) {
                    $destinationPath = 'uploads';
                    $name = Input::get('name');
                    $email = Input::get('email');
                    $extension = Input::file('file')->getClientOriginalExtension();
                    $fileName = Input::file('file')->getClientOriginalName();
                    $displayImage = Input::file('file')->move($destinationPath, $fileName);
                    $user = Profile::create([
                        'name' => $name,
                        'email' => $email,
                        'file_name' => $fileName,
                    ]);

$fileName]));

                    echo 'Upload Succesfully <br>';
           }

                else {
                    Session::flash('error', 'uploaded file is not valid');
                    return Redirect::to('upload');
                }
            }
        }
        else
        {
            echo 'Nothing to upload';

        }

    }

this is working fine but when i change to this it save name and email but not file name what is the problem with this i am failed to find out problem

Profile::create($request->all(['file_name' => $fileName]));

it is possible that it take everything automatically with filename or not with $request->all() function any other solution if you have for me or t is necessary to write every field name ?

Try to do it like this:

$request['file_name'] = $fileName;
Profile::create($request->all());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM