Home > Software design >  How do I get a newly created ID from the Order Controller to the Order Item Controller in laravel
How do I get a newly created ID from the Order Controller to the Order Item Controller in laravel

Time:06-13

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...

  1. Why would you name the Model representation of your invoice_order table project? That should be InvoiceOrder, to A) match the table name and B) meet Laravel naming conventions
  2. Why would order_id be the Primary Key for the invoice_order table? It's typical to use id, or invoice_order_id as the Primary. order_id is close, but just a little off.
  3. 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],
]);
  • Related