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

laravel_ninja's avatar

Laravel Query Builder

While retrieving data from Laravel models

$users = User::where('name', 'John')->get();

$users = User::query()->where('name', 'John')->get();

Which one is recommended and why ?

0 likes
4 replies
Snapey's avatar

They are the same i use query() where I want to spread over many lines

eg

$users = User::where('name', 'John')->latest()->get();

// instead

$users = User::query()
		->where('name', 'John')
		->latest()
		->get();
laravel_ninja's avatar

@Snapey Then what's the purpose of using this I've seen this many times when using scopes but I don't really understand why even we use this.

jlrdw's avatar

@laravel_ninja EIther will work, some folks like eloquent for related data. But it still converts to normal sql and PDO at runtime.

Snapey's avatar

@laravel_ninja sometimes you just need to start the query and then apply other conditions later in the function.

eg;

	$query = User::query();

	// some other logic, or checking something

	$query->where('stuff', $nonsense);

	// some other logic

	$query->where('foo', $bar);

	// later, ready to run the query

	$users = $query->get();

But don't worry about it, Just know that it can be done. When you need something like this, you can give it a try

Please or to participate in this conversation.