Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

a.verrecchia's avatar

Returning Invoice Instance Instead of Model from Laravel Relationship

In my Laravel application, I'm handling the creation of new invoices for customers. Currently, I'm using the following method to create a new invoice directly:

public function create(Customer $customer, array $data): Invoice
{
    return Invoice::create([
        'number' => $data['number'],
        // other invoice fields...
    ]);
}

However, I want to improve this logic using the relationship defined between Customer and Invoice. My goal is to do something like this:

public function create(Customer $customer, array $data): Model
{
    return $customer->invoice()->create([
        'number' => $data['number'],
        // other invoice fields...
    ]);
}

The issue is that the create method on the relationship returns an instance of Model instead of Invoice, even though I'm creating a new invoice. How can I modify my code to directly obtain an Invoice instance instead of Model?

0 likes
6 replies
sidanalavi's avatar

I am assuming you have a Customer model and Invoice model, and Invoice model has relation belongs to customer, So you should create a relation from customer model which will be hasMany relation with invoice.

if you already have that great otherwise make sure you have that

now you can do like below from your controller or anywhere you are accessing customer model

$customer->invoices()->create([
        'number' => $data['number'],
        // other invoice fields...
    ]);

assuming you named hasMany as invoices in customer model it will return newly created invoice

a.verrecchia's avatar

@sidanalavi I already have the relationships set up, but it returns:

Return value is expected to be '\App\Models\Invoice', '\Illuminate\Database\Eloquent\Model' returned
sidanalavi's avatar

@a.verrecchia do you mind sharing you relation function ? and also in which file you have added create method ?

a.verrecchia's avatar

@sidanalavi

I have these two models with their relationships set up. However, when I use the create method in Laravel as mentioned earlier, it returns Model as the type instead of Invoice.

class Invoice extends Model {
	// methods...

    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }
}
class Customer extends Model {
	// methods...

  public function invoices(): HasMany
    {
        return $this->hasMany(Invoice::class);
    }
}
sidanalavi's avatar

Thanks for sharing It shouldn't return Model that's why i asked where you have added public function create() ?

try below in tinker

$customer = Customer::first();
$invoice = $customer->invoices()->create([
// add all your attributes here
]);

it will return Invoice not Eloquent model

Snapey's avatar

why are you expecting to return model?

public function create(Customer $customer, array $data): Model

(return type)

and use invoices() not invoice()

Please or to participate in this conversation.