Laravel 6

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

Avatar de MahmoudKhosravi

Each group has several subgroups.

And I have a filters form like this.


I have a filters table.

public function up()
    Schema::create('filters', function (Blueprint $table) {


    $('#addGroup').click(function () {
        '<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>'+

    $('#addSubGroup').click(function () {
        '<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][]">'+

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,
    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)

I see this message.


So far no problem. What about just storage?

Posté il y a 1 an

Vous ne pouvez pas répondre à ce sujet.