Validation with img upload with laravel

I'm trying to insert data to the database and at the same time uploading an image to the path. It seems the problem is with the validation, but I can't see where is the error. I've been stuck for two days trying to figure out what I'm doing wrong.

Here's the code for the upload:

 protected function store(Request $request)
  {

      $nameFile = null;


      if ($request->hasFile('image')) {


          $name = uniqid(date('HisYmd'));


          $extension = $request->image->extension();


          $nameFile = "{$name}.{$extension}";


          $upload = $request->image->storeAs('categories', $nameFile);

          return $namefile;

      }
  }

Here's the create function:

protected function create(array $data){
     $image = $this->store();
     return Bottleneck::create([
        'setor_id' => $data['setor_id'],
        'mes' => $data['mes'],
        'ano' => $data['ano'],
        'andon' => $data['andon'],
        'conteudo' => $data['coteudo'],
        'atividade_total' => $data['atividade_total'],
        'image' => $image
     ]);
 }

Here's the validation:

 protected function validator(array $data){
     return Validator::make($data , [
        'setor_id' => ['required', 'integer', 'exists:setor_linha,id'],
        'mes' => ['required', 'integer'],
        'ano' => ['required', 'integer'],
        'andon' => ['required', 'string'],
        'conteudo' => ['required', 'string'],
        'atividade_total' => ['required', 'string'],
        'image' => ['image'],
     ]);
 }

The save function:

public function save(Request $request){
     $validation = $this->validator($request->all());
     if($validation->fails()){
         Session::flash('message', 'Error');
        return redirect()->route('bottleneck');
     }else{
        $bottleneck = $this->create($request->all());
        Session::flash('message', 'Success');
        return redirect()->route('bottleneck');
     }
 }

And here's my form:

<form method="POST" action="{{route("create.bottleneck")}}">
                    @csrf
                    <div class="form-group row">
                        <label for="setor_id" class="col-md-4 col-form-label text-md-right">
                            {{__('Production Line')}}
                        </label>

                        <div class="col-md-8">
                            <select id="setor_id" name="setor_id" class="form-control " required>
                                <option>{{__('Select Value')}}</option>
                                    @forEach($line as $data)
                                        <option value={{$data->id}}>{{$data->name}}</option>
                                    @endforEach
                            </select>

                            @if($errors->has('setor_id'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('setor_id')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="mes" class="col-md-4 col-form-label text-md-right">{{__('Month')}}</label>
                        <div class="col-md-8">
                            <input type="number" name="mes" class="form-control">


                            @if($errors->has('mes'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('mes')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="ano" class="col-md-4 col-form-label text-md-right">{{__('Year')}}</label>

                        <div class="col-md-8">
                            <input type="number" name="ano" class="form-control" required>

                            @if($errors->has('ano'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('ano')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="andon" class="col-md-4 col-form-label text-md-right">{{__('Andon')}}</label>

                        <div class="col-md-8">
                            <textarea name="andon" class="form-control" required></textarea>

                            @if($errors->has('andon'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('andon')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="conteudo" class="col-md-4 col-form-label text-md-right">{{__('Content')}}</label>

                        <div class="col-md-8">
                            <textarea name="conteudo" class="form-control" required></textarea>

                            @if($errors->has('conteudo'))
                                <span class="invalid-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('conteudo')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="atividade_total" class="col-md-4 col-form-label text-md-right">{{__('Total Work')}}</label>

                        <div class="col-md-8">
                            <input type="text" name="atividade_total" required>

                            @if($errors->has('atividade_total'))
                                <span class="invalida-feedback" role="alert">
                                    <strong>
                                        {{$errors->first('atividade_total')}}
                                    </strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="image" class="col-md-4 col-form-label text-md-right">{{__('Image')}}</label>

                        <div class="col-md-8">
                            <input type="file" name="image" class="form-control">

                            @if (count($errors) > 0)
                                <div class="alert alert-danger">
                                    <strong>Whoops!</strong> There were some problems with your input.<br><br>
                                    <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                    </ul>
                                </div>
                            @endif
                        </div>
                    </div>

                    <div class="form-group row mb-0">
                        <div class="col-md-8 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                {{__('Save')}}
                            </button>
                        </div>
                    </div>
                </form>

Upvotes: 0

Views: 186

Answers (1)

Jamie Burton
Jamie Burton

Reputation: 515

Your form needs to have the enctype="multipart/form-data" attribute to upload images.

Upvotes: 1

Related Questions