Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with file uploading in laravel

I am try to upload a file with laravel but when submit the form it gives the below error

Exception

Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

here is my blade:

    {{ Form::open(array('route' => 'drivers.store', 'files' => true, 'class' => 'form-horizontal')) }}
    <form role="form">
        <div class="form-group first-field">
            {{ Form::label('first_name', 'First Name:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4 ">
                {{ Form::text('first_name', $value = null, array('placeholder' => 'ex-Jon', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>           
        </div>
        <span class='error-text'> {{ $errors->first('first_name') }} </span>

        <div class="form-group">
            {{ Form::label('last_name', 'Last Name:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('last_name', $value = null, array('placeholder' => 'ex-Doe', 'required' => 'required', 'class' => 'form-control', 'class' => 'form-control')) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('last_name') }} </span>

        <div class="form-group">
            {{ Form::label('email', 'Email:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('email', $value = null, array('placeholder' => '[email protected]', 'rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('email') }} </span>

        <div class="form-group">
            {{ Form::label('contact_number', 'Phone:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('contact_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('contact_number') }} </span>

        <div class="form-group">
            {{ Form::label('sin', 'SIN:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('sin', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('sin') }} </span>

        <div class="form-group">
            {{ Form::label('license_number', 'License Number:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('license_number', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('license_number') }} </span>

        <div class="form-group">
            {{ Form::label('license_file', 'License File:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::file('license_file') }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('license_file') }} </span>

        <div class="form-group">
            {{ Form::label('street_address', 'Street Address:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::text('street_address', $value = null, array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-symbol">* </span>
        </div>
        <span class='error-text'> {{ $errors->first('street_address') }} </span>

        <div class="form-group">
            {{ Form::label('password', 'Password:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::password('password',array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
            <span class="required-text">Between 6 and 12 Characters</span>
        </div>
        <span class='error-text'> {{ $errors->first('password') }} </span>

        <div class="form-group">
            {{ Form::label('password_confirmation', 'Confirm Password:', array('class' => 'col-sm-3 control-label')) }}
            <div class="col-xs-4">
                {{ Form::password('password_confirmation', array('rows' => '3', 'required' => 'required', 'autofocus' => 'autofocus', 'class' => 'form-control' )) }}
            </div>
        </div>

        <div class="form-group">
            {{ Form::submit('Add Driver', array('class' => 'btn btn-primary center-block sh-request-button sign-up')) }}
        </div>
    </form>
{{ Form::close() }}

//controller:

public function store()
    {

        $input = \Input::all();

        ////echo "</pre>";print_r($input); $file= \Input::file('license_file.name');

        $validator = $this->_modelDriver->validator($input);

        if ($validator->fails()) {

            return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
        }

        else {
            echo "test success";exit;
        }

    }

//validator:

public function validator(array $input, $isUpdate=false)
    {
        if(!$isUpdate) {
            $rules = array(
                    'first_name'=>'required|alpha|min:2',
                    'last_name'=>'required|alpha|min:2',
                    'email'=>'required|email|unique:drivers',
                    'password'=>'required|between:6,12|confirmed',
                    'password_confirmation'=>'required|between:6,12'

            );
        } else {
            $rules = array(
                    'firstname'=>'required|alpha|min:2',
                    'lastname'=>'required|alpha|min:2',
                    'email'=>'required|email',
            );
        }

        return Validator::make($input, $rules);

    }

I am new to laravel. So if someone can tell what am I doing wrong and how to fix this.

Thanks

like image 305
user1559230 Avatar asked Mar 22 '14 12:03

user1559230


1 Answers

This is happening because you are trying to return with the file input.

You should write this

$input = \Input::except('license_file');
return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);

instead of only

   return \Redirect::route('drivers.create')->withInput($input)->withErrors($validator);
like image 121
ehp Avatar answered Nov 19 '22 23:11

ehp