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'));
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).
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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community