Laravel 在一对多表中插入数据

Laravel inserting data in one to many tables

开发人员早上好,我正在使用 laravel 和 angular 从事电子商务项目 在 laravel 中,我制作了两个 table(品牌 - 产品),我需要该品牌有很多产品 所以我将此功能添加到产品模型

public function brand()
{
    return $this->belongsTo(Brand::class);
}

并且我将此功能添加到品牌模型

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

第一个问题是我如何处理产品 table 迁移文件中的关系,这是正确的还是有更好的方法?

$table->unsignedBigInteger('brand_id');
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');

第二个问题是我是否应该在 $fillable 产品中定义 brand_id 属性 或不

protected $fillable = ['name', 'brand_id', 'display', 'ram', 'storage', 'rear_cam', 'front_cam', 'img', 'price'];

第三个问题是如何使用 angular 和 laravel

之间的 api 调用来处理商店产品功能并注意 iam
public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'name' => 'required|string|max:100', 'display' => 'required|string|max:100', 'ram' => 'required|string|max:100', 'img' => 'required|image|mimes:jpg,png',
        'storage' => 'required|string|max:100', 'rear_cam' => 'required|string|max:200', 'front_cam' => 'required|string|max:200',
        'price' => 'required'
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors();
        return response()->json($errors);
    }

    $img = $request->file('img');
    $ext = $img->getClientOriginalExtension();
    $name = 'product-' . uniqid() . ".$ext";
    $img->move(public_path('uploads/books/'), $name);

    $name = $request->name;
    $brand_id = $request->brand_id;
    $display = $request->display;
    $ram = $request->ram;
    $storage = $request->storage;
    $rear_cam = $request->rear_cam;
    $front_cam = $request->front_cam;
    $price = $request->price;
    $product = Product::create(['name' => $name, 'brand_id' => $brand_id, 'display' => $display, 'img' => $name, 'ram' => $ram, 'storage' => $storage, 'rear_cam' => $rear_cam, 'front_cam' => $front_cam, 'price' => $price]);

    $product->brand()->sync($request->brand_id);

    $success = 'Product created successfully';

    return response()->json($success);
}

当我尝试存储产品时,它说同步功能有错误,我应该使用什么功能来代替它

实际上你不需要它来同步,在多对多关系中需要一个额外的 table 称为 pivot table,你可以在其中使用附加-分离管理多对多关系-sync 方法,但以防万一您遇到这种情况。 撤消此行

    $product->brand()->sync($request->brand_id);

产品创建成功后。如果您只是将产品功能称为品牌模型。您会看到最后创建的产品在那里。 例如:-

Brand::with('products')->find($brand_id)

谢谢

the first question is how i could handle the relation in the products table migration file, is this right or there is better way?

确实是处理这个问题的好办法

the second questions is should i define the brand_id in the $fillable products property or not

这完全取决于您的用例。通常你只定义用户可以随意修改的属性,比如他的名字。 fillable的概念是为了保护你免受批量赋值。

例如:

  • 如果我将复选框的名称属性从 accept_newsletter 更改为 is_admin
  • 并且如果存在名为 is_admin 的列
  • 并且如果 is_adminfillable 属性中
  • 然后你做了类似的事情:$user->update($request->all());

然后我就有可能成为管理员。

但是,如果 is_admin 不在 fillable 属性中,您必须明确地执行类似:$user->is_admin = $request->is_admin 的操作。哪个更不自然地发生。

在您的情况下,add/edit 产品的人可能无论如何都是管理员,因此您可以将 brand_id 添加到 fillable 属性。

对于您的第三个问题,由于您在 Brand 模型中使用 hasMany 关系,在 Product 模型中使用 belongsTo,因此您只需是 $product->brand()->associate($request->brand_id);.

但请记住:

product = Product::create(['name' => $name, 'brand_id' => $brand_id, 'display' => $display, 'img' => $name, 'ram' => $ram, 'storage' => $storage, 'rear_cam' => $rear_cam, 'front_cam' => $front_cam, 'price' => $price]);

您已经将 brand_id 分配给您正在创建的产品(仅当您将 brand_id 添加到可填写属性时才有效)。将 fillable 属性视为“可以在数组中分配的属性”。