0

I am working on a project using Nova, and I need to create a standard invoice view resource for the invoice model. I am uncertain about the best approach and would appreciate some guidance.

Here's what I have accomplished so far:

  • I have created the invoice model using Laravel Eloquent ORM.
  • I have set up the necessary relationships between the invoice model and other related models (e.g., user, product, etc.).
  • I need to create a resource for the invoice model to display a standard invoice view in my Nova 4 admin panel. the invoice create view will allow me to add new items on the fly to the invoice. I am not sure about the required steps or any specific customizations needed to achieve this.

Could you please provide instructions or resources on how to create a standard invoice view resource in a Nova project?

1 Answer 1

1

You'll likely want to split your setup into two models/resources: Invoice and InvoiceItem. For the Invoice it would be:

public function fields(Request $request) {
     return [
         ID::make()->sortable(),
         BelongsTo::make('User'),
         HasMany::make('InvoiceItems'),  
         DateTime::make('Date Issued'),
         Currency::make('Total Amount', 'totalAmount')->exceptOnForms(),
         // More fields as necessary
    ];
}

The InvoiceItem would then hold the information about the product, quantity, and pricing. The hasMany relation in the Invoice would allow additional InvoiceItems to be added on the fly.

The InvoiceItem might look like:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        BelongsTo::make('Invoice', 'invoice', Invoice::class),
        MorphTo::make('Product', 'productable')->types([
            \App\Nova\Book::class,
            \App\Nova\Electronic::class,
        ]),
        Number::make('Quantity'),
        Currency::make('Price per Item', 'price'),
        Currency::make('Amount')->exceptOnForms()->resolveUsing(function () {
            return $this->quantity * $this->price;
        }),
    ];
}

Note the MorphTo polymorphic relationship allows different models to be added to the invoiceRow.

Calculating the total of the Invoice can be done using a getter in the Invoice model:

public function getTotalAmountAttribute()
    {
        return $this->invoiceItems->sum('amount');
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.