Search code examples
mysqllaravelcontrollerdatabase-migrationlaravel-7

add multiple fields when adding a product


when creating a new product, the user could add more than one size, I want each row to be added in the sizes table, where the foreign key is product_id How should I do that in the product controller?

enter image description here

this is the products controller

 public function store(Request $request)
    {
     
   

         $validatedData = $request->validate([
     'moreFields.*.designation' => 'required',
                    'moreFields.*.buying_price' => 'required',
                    'moreFields.*.selling_price' => 'required',
                    'moreFields.*.weight' => 'required',
                    'moreFields.*.stock' => 'required',
           ]);
    products::create([
            'name' => $request->name,
            
            
            
            'categorie_id' => $request->categorie_id,
           
            'description' => $request->description,
            'user' => (Auth::user()->name),
           
            
        ]);

this is sizes Model

    class sizes extends Model
{
  protected $fillable = [
    'product_id',
    'designation',
    'selling_price',
    'buying_price',
    'stock',
    'weight',
];
      public function products(){
        return $this->belongsTo('App\products'); 
      }
}

This is products Model

class products extends Model
{
   
    protected $guarded=[];
      public function categorie(){
        return $this->belongsTo('App\categories'); 
      }
      public function sizes(){
        return $this->hasMany('App\sizes');
    }
}

This is sizes Migration

public function up()
{
    Schema::create('sizes', function (Blueprint $table) {
        $table->id();
        $table->string('designation');
        $table->decimal('buying_price' );
        $table->decimal('selling_price' );
        $table->decimal('weight');
        $table->unsignedInteger('stock');
        $table->unsignedBigInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->timestamps();
    });
}

THis is products migration

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->bigIncrements('id');
      
        $table->string('name');
      
    
        $table->string('description')->nullable();
   
       
        $table->unsignedBigInteger('categorie_id');
        $table->foreign('categorie_id')->references('id')->on('categories')->onDelete('cascade');
        $table->string('user',300);
       

        $table->softDeletes();
        $table->timestamps();
    });
}

Solution

  • I soved it , in product controller i added this:

      foreach ($request->moreFields as $key => $value) {
    
           sizes::create([
                'product_id' => $product_id,
                'designation'=>$value['designation'],
                'buying_price'=>$value['buying_price'],
                'selling_price'=>$value['selling_price'],
                'stock'=>$value['stock'],
                'weight'=>$value['weight'],
            ]);
        }