Nik
Nik

Reputation: 43

Laravel insert data from one table to another table error

Trying to get the product_id from table one then inserting it to table two, but gives back an error

1048 Column 'product_id' cannot be null (SQL: insert into product_details (product_id, category, brand, provider_id) values (, Peripherals, Riders, 1))

Code:

$product = Product::create([
     'product_name' => $request['product_name'],
     'quantity' => $request['quantity']
]); 

$product->save();
$product_id = $product->id;

$productDetails = ProductDetails::create([
    'product_id' => $product_id,
    'category' => $request['category'],
    'brand' => $request['brand'],
    'provider_id' => $request['provider_id']
]);

Fillable for productdetails model

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

Database structure: (Just remembered, just a while ago I made some changes on my database that was the time when this error popped out.)

Products:

Schema::create('products', function (Blueprint $table) {
            $table->increments('product_id');
            $table->string('product_name');
            $table->integer('quantity');

Product Details:

Schema::create('product_details', function (Blueprint $table) {
            $table->integer('product_id')->unsigned();
            $table->string('category',255);
            $table->string('brand',255);
            $table->integer('provider_id')->unsigned();
            $table->foreign('product_id')
            ->references('product_id')
            ->on('products')
            ->onDelete('cascade');
            $table->timestamps();
        });

This has been solved,

It was just a simple mistake on my part. I should've been using 'product_id' at '$product_id = $product->id' instead of 'id'. Apologies to everyone.

Upvotes: 0

Views: 3151

Answers (5)

Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10061

You will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

Fillable you specify which fields are mass-assignable in your model, So in the model you need to add also product_id:

Model:

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];
////only the field names inside the array can be mass-assign

Automatically assumes your primary key column is going to be id. In order for this to work correctly, you should set your primary key in your model product.php:

protected $primaryKey = 'product_id';

Upvotes: 0

Manzurul Hoque Rumi
Manzurul Hoque Rumi

Reputation: 3094

Before inserting into ProductDetails check if Product is saved or not. Also check your fillable field in your ProductDetails model whether you have added product_id or not.

    $product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]); 

    if($product) {
        $productDetails = ProductDetails::create([
            'product_id' => $product->id,
            'category' => $request['category'],
            'brand' => $request['brand'],
            'provider_id' => $request['provider_id']
        ]);
    }

Update your fillable field like below:

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

Update

If your primary key is product_id, you should call $product->product_id

Upvotes: 2

Tofunmi
Tofunmi

Reputation: 131

You shouldn't run a save() method after running a create; its redundant.

You can run a db transaction in a try and catch to know what exactly is going wrong.

try {
    \DB::transaction(function () use($request) {
        $product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]);

        $productDetails = ProductDetails::create([
            'product_id' => $product->id,
            'category' => $request['category'],
            'brand' => $request['brand'],
            'provider_id' => $request['provider_id']
        ]);
    });

    dd('successful');
} catch (\Exception $e) {
    dd( $e->getMessage() );
}

Upvotes: 0

Jigar
Jigar

Reputation: 3261

I don't think you need to use save() method. after create() method you can get model object and get the id from it directly but make sure your id in db is id not product_id.

$product = Product::create([
            'product_name' => $request['product_name'],
            'quantity' => $request['quantity']
        ]); 

 if($product){
    $productDetails = ProductDetails::create([
        'product_id' => $product->id,
        'category' => $request['category'],
        'brand' => $request['brand'],
        'provider_id' => $request['provider_id']
    ]);
 }else{
    echo "Something went wrong";
 }

Upvotes: 0

Inzamam Idrees
Inzamam Idrees

Reputation: 1981

Hey you have to change your fillable property like this:

protected $fillable = ['product_id', 'category', 'brand', 'provider_id'];

Upvotes: 0

Related Questions