添加产品时添加多个字段

add multiple fields when adding a product

创建新产品时,用户可以添加多个尺寸,我希望每一行都添加尺寸 table,其中外键是 product_id 我应该如何在产品控制器中执行此操作?

这是产品控制器

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

这是尺码型号

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

这是产品型号

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

这是尺寸迁移

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

这是产品迁移

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

你可以这样做

-将create方法的return赋值给一个变量

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

-然后遍历尺寸并将它们分配给该产品

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

这里的变量只是为了展示解决方案,但您需要根据您的要求进行调整

你可以阅读更多相关内容here

我很喜欢它,在产品控制器中我添加了这个:

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