Skip to content
Advertisement

How do I get all the invoice items in my edit page in laravel?

**I am new to laravel, so ignore any mistakes on asking question. I have created the invoice with multiple row table using jquery. Now I need to update the invoice items, so how do I fetch all invoice items in my edit page so I can update it. **

My tables invoices

  • id
  • invoice_no
  • subtotal
  • discount
  • total

invoice_items

  • id
  • invoice_id
  • price
  • qty
  • amount

Invoice model

public function invoice_items()
{
    return $this->hasMany('AppInvoiceItem', 'invoice_id', 'id');
}

InvoiceItem Model

public function invoice()
{
    return $this->belongsTo('AppInvoice');
}

If any resources is needed ask me to provide.

public function edit($id)
    {
        $invoice = Invoice::find($id);
        $invoice_items = InvoiceItem::all();
        return view('invoices.edit', compact('invoice', 'invoice_items'));
    }

Advertisement

Answer

You can try this

$invoice = Invoice::where('id',$id)->with('invoice_items')->first();
return redirect()->route('your blade route name',compact('invoice',$invoice)))

You should get all invoice item that related to invoice .

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement