【问题标题】:store data into pivot table laravel 8将数据存储到数据透视表 laravel 8
【发布时间】:2021-08-29 03:02:44
【问题描述】:

我在类别和产品之间有很多关系 类别模型

class Attribute extends Model implements Auditable
{
    use HasFactory, AuditableTrait;
    protected $fillable = ['category','sub_categ'];
    public function products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class);
    }

}

产品型号

class Product extends Model implements Auditable
{
    use HasFactory, AuditableTrait;
    protected $table = 'products';
    protected $fillable = ['name','price','description', 'details'];
    public function products(): BelongsToMany
    {
        return $this->belongsToMany(Product::class);
    }
}

数据透视表

Schema::create('attributes_products', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->foreignId('product_id')->constrained('products')->onUpdata('cascade')->onDelete('cascade');
            $table->foreignId('attribute_id')->constrained('attributes')->onUpdata('cascade')->onDelete('cascade');
        });

在这之后我应该怎么做,我没有弄清楚附加将如何在数据透视表中工作并将其与产品一起作为 json 响应返回?

编辑

这是我正在处理的架构

我想给每个产品它自己的类别 这是我的产品控制器存储功能

 public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'price' => 'required|numeric',
            'description' => 'required',
            'details' => 'required',
            'stocks' => 'required|numeric',
            //'discounts' => 'required|numeric'
        ]);

        $product = Product::create($request->only('name','price','description', 'details'));
        
        $product->stocks()->create([
            'quantity' => $request->stocks,
            'product_id' => $product->id
        ]);

        $product->discounts()->create([
            //'discount' => $request->discounts,
            'product_id' => $product->id
        ]);

        if($request->hasFile('images'))
        {
            foreach( $request->file('images') as $file)
            {
                $file->store('public/products');
                $product->images()->create([
                    'product_id' => $product->id,
                    'file_path'  => $file->hashName()
                ]);
            }
        }

        $product->categories()->sync([
            'product_id' => $product->id,
            'attribute_id'=> 1
        ]);
    }

【问题讨论】:

  • 这可能会对您有所帮助。类似于您的要求stackoverflow.com/questions/67919919/…
  • @JohnLobo 抛出 500 错误,无法识别数据透视表
  • 你能展示你在控制器方法中做了什么吗?还有 dd($request->all());所以我们可以帮助您真正需要改变的地方
  • enable debug true in env 这样你就可以得到准确的错误

标签: php laravel relationship


【解决方案1】:

在您的产品模型中检查您的关系。

public function categories()
{
    return $this->belongsToMany(Category::class);
}

通常数据透视表只需要 2 个 ID。所以在你的情况下只有两列:product_id & category_id

按照惯例,你的表名应该是category_product,否则,你应该在关系的第二个参数上指定它。

  • 也解决这个问题,update 有错字:

    $table->foreignId('attribute_id')->constrained('attributes')->onUpdate('cascade')->onDelete('cascade');

  • 最后附上:

    $product = Product::find(1);

    $product->categories()->attach($categoryId);

在文档中也有很好的解释:https://laravel.com/docs/8.x/eloquent-relationships

【讨论】:

  • 从哪里可以得到 $categoryId var ?
  • nvm 我想通了
猜你喜欢
  • 2019-04-07
  • 2020-09-01
  • 2019-06-12
  • 2018-04-10
  • 2021-09-20
  • 2021-08-24
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多