Since I plan on having more fields to juggle on creation/update, I was hoping someone here could point me in the right direction in terms of best practice for queries using Eloquent?
It would be pretty nice to use all of the methods found on the user model to update, but I am concerned that doing it the way you see above is not optimal (i.e., too many queries).
Is it possible for me to chain the model’s methods since I return $this? Optimally, I would like to make sure I use as few queries as possible.
Much of it will come down to personal preference, and Eloquent is fairly good at not executing needless queries. Personally, I set the $fillable attributes on the model then set them all by passing all of the request input to the fill method.
$user = new User;
$user->fill($request->getParsedBody());
$user->save();
Setting the $fillable fields helps ensure only the input you care about is saved to the model.
There are a couple of different techniques to see the query log. One is to turn it on at the database level. That is often the quickest and easiest. Otherwise have a look in this thread about using the Tracy debugbar with Slim which also gives some info on displaying queries from Eloquent.
Awesome tips! Thanks os much @tflight, I really appreciate your quick reply and help!
That’s a great tip on using fill() and getParseBody()! Thanks! I am playing with that now.
Also, thank you for note on the query log. I will try the database level first; Tracy looks cool though, so I will also explore using that over the next few days as I continue my dev work.
Thanks a bunch! I’ll let you know if I discover anything new and/or have any questions.