Dina Shaldoum Dina Shaldoum -4 years ago 88
MySQL Question

insert data in multiple table with one function in laravel

I'm trying to add values in multiple tables with the same function but I get an error that the id and product_id can't be null !! even though they are set. Here's my code:

$parentproduct=new Product();
$parentproduct->id=Input::get('id');
$insertedId = $parentproduct->id;
$parentproduct->save();

$product=new ProductsTranslation();


$product->id=Input::get('id');
$product->product_id =Input::get('insertedId');
$product->title=Input::get('title');
$product->content=Input::get('content');
$product->price=Input::get('price');
$product->description_title=Input::get('description_title');
$product->prod_info_title=Input::get('prod_info_title');
$product->prod_info=Input::get('prod_info');
$product->save();

Answer Source

Looks like you need to move a few things around here...

This $insertedId = $parentproduct->id; wont return a value until you've ran `->save().

Also, your second statement is trying to get an Input::('insertedId') but you're setting a variable above.

Try this:

$parentproduct = new Product();
$parentproduct->id = Input::get('id');
$parentproduct->save();
$insertedId = $parentproduct->id;

$product = new ProductsTranslation();   
$product->id = Input::get('id');
$product->product_id = $insertedId;
$product->title = Input::get('title');
$product->content = Input::get('content');
$product->price = Input::get('price');
$product->description_title = Input::get('description_title');
$product->prod_info_title = Input::get('prod_info_title');
$product->prod_info = Input::get('prod_info');
$product->save();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download