Home > OS >  how to insert muti select to database
how to insert muti select to database

Time:08-31

Look at my code snippet. i want to insert all values 1,2,3 to database i have color table and product table

These are My Tables

colors: | A header | Another header | | -------- | -------------- | | First | row | | Second | row |

product: | id| name| color_id| |---- |------| -----| | 1 | Phone| 1 | | 2 |Laptop| 2 |

in the product table How can i select more than one color to the product i tried to make this but it failed | id| name| color_id| |---- |------| -----| | 1 | Phone| 1 3 | | 2 |Laptop| 2 4 |

i want to give the product two colors because i want to show the colors the product is has using the code in ColorController

These are My Controllers

ColorController:

         class productColorController extends Controller {
                  ...

           public function store(Request $request) {
     $colors = collect($request->color); // Here it gives the color are getting from the multi select you can see it in code snippet
    
         $color = productColor::create([ 
       'color' => $colors['color'], //  here i want to create all of the color are getting from request and save to database but when i try this i see only one color was saved to database
        
       ]); 
    
    }
// The code to show all colors
$getcolor = productColor::where('id' , 1)->get(); // I Want To Show All Colors User selected in id 1
    }

The conclusion of the question: when user selected multi colors i want to save it in database and show all the colors to admin

<select data-placeholder="Please Choose Color" multiple name="" >
                                        <optgroup label="Please Choose Color">
                                          <option value='black'>
                                          Black
                                          </option>
                                           <option value='white'>
                                          White
                                          </option>
                                        
                                           <option value='gray'>
                                          Gray
                                          </option>
                                           <option value='blue'>
                                          Blue
                                          </option>
                                        
                                       >
                                        </optgroup>
                                    </select>

CodePudding user response:

So you want a product to have more than one color? In this case you can build a pivot table. Basically it's an intermediate table between 2 tables and you can assign more than 1 color to your product in this case.

    Schema::create('color_product', function(Blueprint $table)
    {
        $table->integer('color_id');
        $table->integer('product_id');
    });

In Product Model:

public function colors()
{
return $this->belongsToMany(Color::class);
}

In color Model:

public function products()
{
return $this->belongsToMany(Product::class);
}

To retrieve the relationships:

IE:

$product = Product::first();
$product->colors;

More on documentation: https://laravel.com/docs/9.x/eloquent-relationships#many-to-many

CodePudding user response:

add a name to select element and it is better to create another table for colors and create a realationship between product table and color table

<select data-placeholder="Please Choose Color" multiple name="color[]"

and this article will help you How to Store Multiple Select Values in Laravel?

  • Related