What I want to do is pass a newly created auto increment OrderID from my Order Controller Create Form to the OrderItem Controller Create Form, and add Order_Items related to that specific OrderID. i want to create iditems when i create an order which order_id is auto increament in database. my store code in ordercontroller
public function store(Request $request)
{
$input = $request->all();
project::create($input);
$g = project::orderBy('order_id', 'desc')->first();
$id= new items;
$id->order_id = $g;
$id->save();
items::create($input);
return redirect('/project')->with('insert','f');
}
item model code
class items extends Model
{
protected $table = 'invoice_order_item';
protected $primaryKey = 'order_item_id';
protected $fillable = [ 'order_id' , 'item_code', 'item_name', 'order_item_quantity', 'order_item_price', 'order_item_final_amount'];
}
order model code
class project extends Model
{
protected $table = 'invoice_order';
protected $primaryKey = 'order_id';
protected $fillable = ['order_receiver_name', 'order_receiver_address', 'order_total_before_tax', 'order_total_tax',
'order_tax_per', 'order_total_after_tax', 'order_amount_paid', 'order_total_amount_due', 'note'];
}
CodePudding user response:
Model::create()
returns the newly created Database Model, including its primary key. All you need to do is assign project::create()
to a variable, and reference it later:
$project = project::create($input);
...
$items = new items();
$items->order_id = $project->order_id;
$items->save();
// items::create($input); // Drop this line; you already created an `items` model via `new items(); ... $items->save()`
That being said, there's a lot of issues with your code...
- Why would you name the Model representation of your
invoice_order
tableproject
? That should beInvoiceOrder
, to A) match the table name and B) meet Laravel naming conventions - Why would
order_id
be the Primary Key for theinvoice_order
table? It's typical to useid
, orinvoice_order_id
as the Primary.order_id
is close, but just a little off. - You can use relationships to create these too:
$project = project::create($input);
$items = $project->items()->create($input);
You would need to define the relationship between them though, which can be done by reading the documentation:
https://laravel.com/docs/9.x/eloquent-relationships
Corrected Code:
public function store(Request $request) {
$inputs = $request->input()
$invoiceOrder = InvoiceOrder::create($inputs);
$invoiceOrderItem = $invoiceOrder->invoiceOrderItems()->create($inputs);
}
app\Models\InvoiceOrder.php
:
class InvoiceOrder extends Model {
protected $table = 'invoice_order';
protected $primaryKey = 'order_id';
protected $fillable = [
'order_receiver_name',
'order_receiver_address',
'order_total_before_tax',
'order_total_tax',
'order_tax_per',
'order_total_after_tax',
'order_amount_paid',
'order_total_amount_due',
'note'
];
public function invoiceOrderItems() {
return $this->hasMany(InvoiceOrderItem::class);
}
}
app\Models\InvoiceOrderItem.php
:
class InvoiceOrderItem extends Model {
protected $table = 'invoice_order_item';
protected $primaryKey = 'order_item_id';
protected $fillable = [
'order_id',
'item_code',
'item_name',
'order_item_quantity',
'order_item_price',
'order_item_final_amount'
];
public function invoiceOrder() {
return $this->belongsTo(InvoiceOrder::class);
}
}
CodePudding user response:
You may use the createMany method to create multiple related models:
$order = Order::create($request->all());
$orderItems = $order->orderItems()->createMany([
['amount' => 55.50],
['amount' => 99.20],
]);