Support the ongoing development of Laravel.io →
Eloquent Architecture
Last updated 1 year ago.
0

While I am in no position to comment on best practice, I can let you know that this already exists in Laravel if you use Eloquent so you don't need to recreate the wheel. Just pass in an array of columns

From the documentation:

 // Retrieve the user by the attributes, or create it if it doesn't exist...
 $user = User::firstOrCreate(array('name' => 'John'));
Last updated 1 year ago.
0

In short, no.

There is no definitive answer but try to avoid instaniating a model within a model because it cannot be tested (phpUnit). Inject the model instead (IE: pass as a parameter).

Last updated 1 year ago.
0

landjea said:

While I am in no position to comment on best practice, I can let you know that this already exists in Laravel if you use Eloquent so you don't need to recreate the wheel. Just pass in an array of columns

From the documentation:

// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));

Thanks! I really should read through the documentation in more detail I guess.. ;)

jacksoncharles said:

In short, no.

There is no definitive answer but try to avoid instaniating a model within a model because it cannot be tested (phpUnit). Inject the model instead (IE: pass as a parameter).

Ok, understood - So would I instantiate the model in the controller then?

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.