I have set up a cproduct table in my Laravel project with 3 foreign keys. Now I want to populate this table using Postman. Can anyone guide me on how to achieve this?
This is the structure of the c_products table:
public function up()
{
Schema::create('c_products', function (Blueprint $table) {
$table->bigIncrements('cproduct_id');
$table->string('name');
$table->string('file_path');
$table->integer('price');
$table->foreignId('category_id')->nullable()->constrained('p_categories');
$table->foreignId('cactus_id')->nullable()->constrained('cacti');
$table->foreignId('id')->nullable()->constrained('users');
$table->timestamps();
});
}
In my CProductController, I have written code to add data to the cproducts table:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\CProduct;
class CProductController extends Controller
{
//
function addCProduct(Request $req)
{
$cproduct=new CProduct;
$cproduct->name=$req->input('name');
$cproduct->price=$req->input('price');
$cproduct->color=$req->input('color');
$cproduct->file_path=$req->file('file')->store('c_product');
$cproduct->save();
return $cproduct;
}
}
I believe I need to include the foreign key fields in the "function addCProduct". Could someone provide me with the necessary code for this?
Your assistance is greatly appreciated!