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

oroalej's avatar

Purpose of Builder query

Hi,

Just want to ask what is the purpose of Builder in

$model::when(condition, function(Builder $query){
    $query->someCondition;
})->get();

Because sometimes I get an error using it and by removing it fixed the issue.

0 likes
1 reply
Nash's avatar
Nash
Best Answer
Level 20

It's called type hinting. You are telling PHP that the $query argument must be of type Builder (an instance of Illuminate\Database\Eloquent\Builder). If you are getting an error, you probably forgot to add use Illuminate\Database\Eloquent\Builder at the top of your file.

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

In this case, you are requiring a specific type of object but you can also do it for other types, like int or array. Depending on your use case, it might help with readability or to make sure that you cannot pass an argument of the wrong type to the method by accident:

public function example(int $count, array $data)
{
    // $count MUST be an integer (or convertible to an integer) 
    // $data MUST be an array
}
2 likes

Please or to participate in this conversation.