We’ll have a look at the method https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Eloquent/Builder.php#L290
Mar 15, 2023
5
Level 1
Curious about Query Builder "where"
In the api doc, the where method is declare as where($column, $operator = null, $value = null, $boolean = 'and'), but we can use it without passing the $operator. This make me wonder how can it work when we call the method, for example:
$model->where('foo', $bar);
// instead of
$model->where('foo', value: $bar);
Normally in other language, eg Java, it make sense because we declare the same method twice as shown:
public static FooClass bar(string column, string operator, string value){}
public static FooClass bar(string column, string value){}
Level 24
@shen
User::get('id', 'name');
func_get_args() is turns the params string to array
so when we create a function
public function get($columns)
{
$columns = is_array($columns) ? $columns : func_get_args();
if (count($columns) == 2) {
// return something
}
// keep going
}
->get('a', 'b', 'c')
or
->get(['a', 'b', 'c'])
will get the same result ['a', 'b', 'c']
passing array or string also work
1 like
Please or to participate in this conversation.