Sara Tabbassi
Sara Tabbassi

Reputation: 81

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();
    });
}

Upvotes: 1

Views: 176

Answers (2)

Sara Tabbassi
Sara Tabbassi

Reputation: 81

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'],
        ]);
    }
 

Upvotes: 1

omar esmaeel
omar esmaeel

Reputation: 572

You could do so

-assign the return of create method to a variable

$product = products::create([
        'name' => $request->name,
        'categorie_id' => $request->categorie_id,
        'description' => $request->description,
        'user' => (Auth::user()->name),
    ]);

-and then loop through sizes and assign them to that product

    foreach($yourItirator as $size)
    {
    $product->sizes()->create([
    ['designation'=> $size->destination,
    'selling_price'=> $size->selling_price,
    'buying_price'=> $size->buying_price,
    'stock'=> $size->stock,
    'weight'=> $size->weight]
]);
}

The variables here are just to showcase the solution, but you need to adjust it according to your request

you could read more about it here

Upvotes: 1

Related Questions