lundi 23 juillet 2018

Laravel 5.1 $request->file return null

I'm currently working on a simple form to let users upload a name and an image. Unfortunately, the $request->input('image') and Input::file('image') return null. If I do $request->string('image') the name of the file uploaded is returned. Can someone explain to me what I am doing wrong?

Form

{!! Form::open(['url' => 'categories']) !!}
    <div class="form-group">
        {!! Form::label('name', 'Naam') !!}
        {!! Form::text('name') !!}
    </div>
    <div class="form-group">
        {!! Form::file('image', null) !!}
    </div>
    {!! Form::submit('Create Category!', array('class' => 'btn btn-primary')) !!}
{!! Form::close() !!}

Controller function:

    public function store(Request $request)
    {
        $rules = [
            'name'       => 'required',
            'image'      => 'required'
        ];

        $validator = Validator::make(Input::all(), $rules);

        /**
        * Adding the code here also returns null../
        * 
        $file = $request->file('image');
        dd( $request->file('image'));
        * 
        */

        // process the login
        if ($validator->fails()) {
            return Redirect::to('categories/create')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {

            $file = $request->file('image');
            dd( $request->file('image'));
            $destination = public_path() . '/img/';
            $name = Carbon::now()->toDateTimeString();
            $extension = $file->getClientOriginalExtension();
            $filename = $name . '.' . $extension;
            $file->move($destination, $filename);

            $category = new Category();
            $category->name       = Input::get('name');
            $category->file_path = $filename;
            $category->save();

            Session::flash('message', 'Successfully created Category!');
            return Redirect::to('categories');
        }

    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire