Individual model methods vs bulk query update: Best approach?

I am using Eloquent.

I have a user model with methods like setName(), setEmail() and setPassword(). For example:

public function setPassword($password) {
	$this->update([
		'password' => password_hash($password, PASSWORD_DEFAULT),
	]);
	return $this;
}

In other parts of my code, I having trouble deciding what is the best way to handle creating/updating the user model.

I can either do this:

	//$this->auth->user()->update([
	// or:
	$user = User::create([
		'email' => $request->getParam('email'),
		'name' => $request->getParam('name'),
		'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
	]);

… or this:

	$user = User::create([
		'email' => $request->getParam('email'),
		'name' => $request->getParam('name'),
	]);
	$user->setPassword($request->getParam('password'));

… or this:

	$user = User::create();
	$user->setName($request->getparam('name'));
	$user->setEmail($request->getparam('email'));
	$user->setPassword($request->getParam('password'));

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.

Ah, both of these blocks of code appear to work:

	$user = User::create()
		->setName($request->getparam('name'))
		->setEmail($request->getparam('email'))
		->setPassword($request->getParam('password'));

… and:

	$this->auth->user()
		->setName($request->getparam('name'))
		->setEmail($request->getparam('email'));

Looks like because I return $this from my user model’s methods, that allows me to chain like you see above.

Generally speaking, is this the best way to do it?

I have yet to figure out how to see a log of my queries, so that should tell me more about what is going on underneath the ORM hood.

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.

2 Likes

Awesome tips! Thanks os much @tflight, I really appreciate your quick reply and help! :wink:

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.