Laravel 6

updateOrCreate

Avatar de MahmoudKhosravi
MahmoudKhosravi

This form is an edit. I want to update the form.

one

I added another one 7 GB and hit save.

two

And later I see that it is stored like this. While it should have two values 8 GB AND 7 GB.

Edit

I have several groups and several subgroups. How do I update multiple subgroups?

And I might add a few subgroups. How do I save it?

three

public function update(Request $request, Filter $filter)
{
    $filters = collect($request->filters);
    $filters->each(function ($item) use ($request, $filter) {
        if (isset($item['name']) && isset($item['latin'])) {
            $data = [
                'category_id' => $request->category_id,
                'name' => $item['name'],
                'latin' => $item['latin'],
                'field' => $item['field'],
            ];
            $filter_item = Filter::query()->firstOrCreate(['id' => $item['id']]);
            $filter_item->save();
        } else {
            return null;
        }
        if (isset($item['value'])) {
            foreach ($item['value'] as $value) {
                if (count(array($value))) {
                    $filter_item->values()->updateOrCreate(
                        ['id' => $filter_item->id],
                        ['value' => $value]
                    );
                }
            }
        }
    });

edit.blade.php

    function addChildFilter(groupId) {
        count++;
        let selectedField = $('#group' + groupId).find('.field').first().val();
        console.log(selectedField);
        if (selectedField == 0) {
            let html = '<div class="w-100">' +
                '<div class="col-md-4 mb-3 position-relative">' +
                '<i class="fas fa-times text-danger position-absolute end-0 cursor-pointer"></i>' +
                '<label for="value">value</label>' +
                '<input type="text" id="value" class="form-control" name="filters['+groupId+'][value][]">' +
                '</div>' +
                '</div>';
            $('#group' + groupId).find('.showGroups').append(html);
        }

When I add dd

public function update(Request $request, Filter $filter)
{
    dd($request->all());

I see this message

array:4 [▼
  "_token" => "9BslBOlbFVrgLihqicK1EUa1Js4HctyKdAgAMDni"
  "_method" => "PUT"
  "category_id" => "37"
  "filters" => array:2 [▼
	0 => array:5 [▼
	  "id" => "1"
	  "name" => "Ram"
	  "latin" => "ram"
	  "field" => "0"
	  "value" => array:1 [▶]
	]
	1 => array:1 [▼
	  "value" => array:1 [▼
		0 => "8 GB"
	  ]
	]
  ]
]

Vous ne pouvez pas répondre à ce sujet.