tasninta's avatar

Image will not update

Hi I am trying to update my image field for my product_entities. The image I'm trying to upload is not in the files parameter.

I hope someone can help me out to save my image with the update function

View:

@section('content')
    <div class="container">
        <form method="post" action="{{ action('ProductEntityController@update', $entity->id) }}">
            @csrf
            @method('PUT')
            <div class="col-md-5">
                <div class="form-group">
                    <label for="name">{{'Price:'}}</label>
                    <input type="text" class="form-control" name="price" value="{{$entity->price}}">
                </div>

                <div class="form-group row">
                    <label for="product" class="col-sm-4 col-form-label text-md-right">Choose a product</label>
                    <div class="col-md-6">
                        <select id="product" name="product_id[]" multiple="multiple">
                            @foreach($products as $id => $name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="color">{{'Color:'}}</label>
                    <input type="text" class="form-control" name="color" value="{{$entity->color}}">
                </div>

                <div class="form-group">
                    <label for="name_unit">{{'Unit name:'}}</label>
                    <input type="text" class="form-control" name="name_unit" value="{{$entity->name_unit}}">
                </div>

                <div class="form-group">
                    <label for="unit">{{'Unit:'}}</label>
                    <input type="text" class="form-control" name="unit" value="{{$entity->unit}}">
                </div>

                <div class="form-group">
                    <label for="amount">{{'Amount:'}}</label>
                    <input type="text" class="form-control" name="amount" value="{{$entity->amount}}">
                </div>

                <div class="form-group">
                    <label for="image">Add a image</label>
                    <input type="file" class="form-control-file" id="image" name="image">
                </div>

                <button type="submit" class="btn btn-primary">Update</button>
            </div>
        </form>
    </div>
@endsection

Controller:

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ProductEntity $entity)
    {
      $this->admin($request);

      $rules = [
        'price' => 'required|between:0,99.99',
        'color' => '',
        'name_unit' => '',
        'unit' => '',
        'amount' => '',
      ];

      dd($request);
      exit;

      $validator = Validator::make($request->all(), $rules);

      if ($validator->fails()) {
        return redirect()
          ->action('ProductEntityController@edit', ['id' => $entity->id])
          ->withErrors($validator)
          ->withInput();
      }

      if ($request->hasFile('image')) {
        $image = $request->file('image');
        $name = $image->getClientOriginalName();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $entity->price = $request->input('price');
        $entity->color = $request->input('color');
        $entity->name_unit = $request->input('name_unit');
        $entity->unit = $request->input('unit');
        $entity->amount = $request->input('amount');
        $entity->image = $name;
        $entity->save();
        $entity->products()->sync($request->get('product_id'));
      }

      $entity->price = $request->input('price');
      $entity->color = $request->input('color');
      $entity->name_unit = $request->input('name_unit');
      $entity->unit = $request->input('unit');
      $entity->amount = $request->input('amount');
      $entity->save();
      $entity->products()->sync($request->get('product_id'));

      return redirect('entities')->with('status', 'Entity has been edited!');
    }
0 likes
1 reply
tasninta's avatar
tasninta
OP
Best Answer
Level 1

Nevermind I forgot to add the

enctype="multipart/form-data"

In the form.

Please or to participate in this conversation.