Tuesday, April 4, 2017

Error on multiple upload, Laravel 5.4

please understand me: I am yet learning to code, have no experience. Trying to code, but sometimes I get stuck, like now. So I need to ask wise men :)

I am trying to make multiple file upload with Laravel 5.4. At the moment I get this error, when submit:

ErrorException in Factory.php line 91: Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, null given, called in C:\xampp\htdocs***\vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php on line 102 and defined

Form here:

{!! Form::open(['method' => 'POST', 'route' => ['posts.store'], 'files' => true,]) !!}
    <div class="row">
        <div class="col-xs-12 form-group">
            {!! Form::label('title', 'Title*', ['class' => 'control-label']) !!}
            {!! Form::text('title', null, ['class' => 'form-control']) !!}
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 form-group">
            {!! Form::label('description', 'Description*', ['class' => 'control-label']) !!}
            {!! Form::textarea('description', null,  ['class' => 'form-control']) !!}
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 form-group">
            {!! Form::file('fotos[]', array('multiple'=>true), [ 'class' => 'form-control']) !!}
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 form-group">
            {!! Form::submit('Create', ['class' => 'btn btn-primary']) !!}
        </div>
    </div>
    {!! Form::close() !!}

Controller (store):

public function store(PostRequest $request) { $input = $request->all();

if($input->hasFile('fotos[]'))
{
    foreach ($request->file('fotos[]') as $file) {
        $name = time() . $file->getClientOriginalName();
        $file->move('/images/', $name);
        Foto::create(['post_id'=>$post->id, 'filename'=>$name]);

    }
}

$post->fotos()->create($input);

return redirect()->route('posts.index');

}

Foto model:

public function posts() {
    return $this->belongsTo('App\Post'); }

Post model:

public function fotos() {
    return $this->hasMany('App\Foto');}

PostRequest:

{public function rules()
[
        'title' => 'required',
        'description' => 'required'
    ];} 

To be honest I don't know if code works, I just want to solve that error on top and understand what I did wrong, so I can fix it, learn what it was and continue.



via G Strato

Advertisement