Laravel 6

How to save subgroups in the filters table with laravel 7?

Avatar de MahmoudKhosravi
MahmoudKhosravi

Each group has several subgroups.

And I have a filters form like this.

demo

I have a filters table.

public function up()
{
    Schema::create('filters', function (Blueprint $table) {
        $table->id();
        $table->foreignId('category_id')->constrained()->cascadeOnDelete();
        $table->integer('parent_id');
        $table->string('name')->nullable();
        $table->string('latin')->nullable();
        $table->smallInteger('field')->nullable();
        $table->string('checkbox_name')->nullable();
        $table->string('color_name')->nullable();
        $table->string('color_code')->nullable();
        $table->timestamps();
    });
}

create.blade.php

<script>
    $('#addGroup').click(function () {
        count++;
        '<input type="text" id="name" class="form-control" name="filters['+count+'][name]">'+
        '<input type="text" id="latin" class="form-control" name="filters['+count+'][latin]">'+
         '<select id="field" class="form-select field" name="filters['+count+'][field]">'+
            '<option value="1">Check Box</option>'+
            '<option value="2">Color</option>'+
         '</select>'+
    });

    $('#addSubGroup').click(function () {
        count++;
        '<input type="text" id="checkbox_name" class="form-control" name="filters['+count+'][checkbox_name][]">' +
        '<input type="text" id="color_name" class="form-control" name="filters['+count+'][color_name][]">'+
         '<input type="text" id="color_code" class="form-control" name="filters['+count+'][color_code][]">'+
    });
</script>

How to save checkbox_name, and color_name and color_code.

public function store(Request $request)
{
    $filters = collect([]);
    if ($request->filters) {
        foreach ($request->filters as $filter) {
            if ($filter['name'] != null && $filter['field'] != null) {
                $model = Filter::updateOrCreate([
                    'id' => $request[0],
                ], [
                    'name' => $filter['name'],
                    'latin' => $filter['latin'],
                    'field' => $filter['field'],
                    'category_id' => $request->category_id,
                    'parent_id' => 0,
                ]);
                $filters->push($model);
            }
        }
    }
    Filter::whereNotIn('id', $filters->pluck('id'))->delete();
    return redirect()->back();
}

I see in the database but the checkbox_name, and color_name and color_code, are blank yet.

My Question

How to save parent_id each subgroup to id of groups.

I want to save all data in the filters table. I do not want to save data in multiple tables.

When I add dd in the store method.

public function store(Request $request)
{
    dd($request->all());

I see this message.

dd

So far no problem. What about just storage?

Avatar de MahmoudKhosravi
MahmoudKhosravi

???????????????????????????????????

ANSWER MY QUETION

Avatar de MahmoudKhosravi
MahmoudKhosravi

???????????????????????????????????

ANSWER MY QUETION

Vous ne pouvez pas répondre à ce sujet.