В моей таблице product
у меня есть поле product_id
, которое не является primary key
, куда я хочу вставить category name
с 3-значным уникальным идентификатором в это поле, используя laravel ORM. Пожалуйста, помогите мне, как я могу это реализовать
Таблица категорий
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('description');
$table->timestamps();
});
Таблица продуктов
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('product_id');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('name', 100);
$table->float('price', 5);
$table->text('description');
$table->timestamps();
});
Способ хранения товара
public function store(Request $request)
{
$product = new Product();
$product->product_id = **How can I implement that? (Example: Category-321)**
$product->name = $request->name;
$product->category_id = $request->category;
$product->price = $request->price;
$product->description= $request->description;
$product->save();
return redirect()->route('product.index');
}