jeudi 25 juin 2020

Complete the form with data from the Laravel database

I am beginner in php and Laravel. I have small problem with fill forms data from database. At the moment I am trying to complete my form with data from the database. The whole looks as follows:

@foreach($countertops as $value)
                                            @foreach ($value->features as $feature)
                                                @if ($feature->key == 'countertop_type')
                                                    @if ($feature->description == 4)
                                                        <input type="hidden" name="countertop[][id]" value="">
                                                        <div class="form-group row">
                                                            <div class="col-lg-4 col-md-4 col-sm-12 text-right pt-2">
                                                                <input type="checkbox"
                                                                       class="xxxxxx mr-2"
                                                                       name="countertop[][checkbox]"  @if(!empty($product) && (!empty($selectedCountertops)) && (!empty($selectedCountertops['countertop['.$value->id.'][checkbox]'])) && old('countertop['.$value->id."][checkbox]", $selectedCountertops['countertop['.$value->id.'][checkbox]'])) checked @endif
                                                                       value="1" >
                                                                <label></label>
                                                            </div>
                                                            <div class="col-lg-4 col-md-4 col-sm-5">
                                                                <input type="text"
                                                                       name="countertop[][min]"
                                                                       class="form-control currencyMask"
                                                                       value=""
                                                                       placeholder="Wpisz minimalną długość">
                                                            </div>
                                                            <div class="col-lg-4 col-md-4 col-sm-5">
                                                                <input type="text"
                                                                       name="countertop[][max]"
                                                                       class="form-control currencyMask"
                                                                       value=""
                                                                       placeholder="Wpisz maksymalną długość">
                                                            </div>
                                                        </div>
                                                    @endif
                                                @endif
                                            @endforeach
                                        @endforeach

The form display works correctly. The problem is only in:

  1. checking / unchecking the checkbox (checked when status == 1 or unchecked when == 0 or no exist)
  2. entering the maximum value in input (maxvalue)
  3. entering the minimum value in input (minvalue)

In controller I Have:

$selectedCountertops = $selectedCountertopsRepository->getSelectedItems($id);

Where getSelectedItems is:

....
public function getSelectedItems(int $id)
    {
        return $this->model->where('product_id', $id)->orderBy('id', 'ASC')->get();
    }
...

This return me:

Illuminate\Database\Eloquent\Collection {#1568 ▼
  #items: array:2 [▼
    0 => App\Models\SelectedCountertops {#1569 ▼
      #quarded: array:1 [▶]
      #fillable: array:5 [▶]
      #connection: "mysql"
      #table: "selected_countertops"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [▼
        "id" => 1
        "countertops_id" => 8
        "product_id" => 10
        "status" => "1"
        "maxvalue" => "88.00"
        "minvalue" => "99.00"
        "created_at" => "2020-06-25 09:44:17"
        "updated_at" => "2020-06-25 09:44:17"
      ]
      #original: array:8 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => App\Models\SelectedCountertops {#1570 ▶}
  ]
}

This is my model:

class SelectedCountertops extends Model
{
    use ScopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['product_id', 'status', 'maxvalue', 'minvalue', 'countertops_id'];

}

in blade file: $value->id is countertops_id from my model

How can I repair it?



via Chebli Mohamed

1 commentaire:

officalseo786@gmail.com a dit…

How To Insert Check Box Value Using Ajax In Laravel

Using Ajax is another way to Store a checkbox value with json in laravel. Here you can see, we are using controller, model and send response to ajax via json..

For More Info:- How To Insert Check Box Value Using Ajax In Laravel

Enregistrer un commentaire