Why I Sometimes Use query() Method?

preview_player
Показать описание
A short answer to the question I get. Why User::query() if it doesn't add any functionality?

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

query() also helps to auto suggest the methods of builder class as if you use model instance without query() it will not suggest all the builder methods you can chain like: when, where, paginate etc.

shubhamc
Автор

I use query() almost every time for two reasons: 1. Readability, as you also said. Many developers think less code is better code and that is not true. Readability is more important then having less code. 2. not having PHP Storm underlining first method and being able to CTRL + click on the methods :)

the-code-reviewer
Автор

I remember having hypertension once because of this, so i thought this might be a good idea to write it here so others doesn't fall for this like i did
Assuming we have a User model in App\Models namespace with soft delete enabled, the statement DB::table("users") doesn't produce the same output as User::query()
This is because the table() method will allow all users to be queried, while the query() method will add a condition to exclude soft deleted users from all queries:
DB::table("users")->dd(); // Produces select * from `users`
User::query()->dd(); // Produces select * from `users` where `users`.`deleted_at` is null

EyadMohammedOsama
Автор

We always use query() for code completition. Meanwhile I also recommend using arrow function for shorter code:
$users = User::query()
->when(request('name'), fn ($q, $name) => $q->where('name', 'like', '%' . $name . '%'));

kukelattila
Автор

Oh, hell yes. I have a query that I found so hard to implement with eloquent that I abstracted it to a service class that I pass parameters to and it returns the results. It had joins and whens and wheres and all kinds of stuff. The point is that it was easier to *reason* about. I think it's good practice to use the base query builder when eloquent is just too abstract which can be confusing. It brings you closer to the actual database query. It's often more readable and allows you to do sanity checks in your sql terminal.

rosselliot
Автор

i think query is useful since it provides flexibility in lettting progressively build out your query in successive steps . it used it to implement something similar to spatie filter and it was really straight forward. It is really really a good tool to have in your toolbox

tinmancode
Автор

I've been waiting for this, as always, thanks for the tips.

amirulidzham
Автор

query() is also useful for ide autocomplete

vrstudio
Автор

when() function was new to me, thanks, i was using if statement instead

abdulbasitsalah
Автор

Sometimes in services for filtering, I pass a query builder into the constructor so I sometimes do Model::query() or Model::with('relation')

konqi
Автор

Readability, yes. Also, I prefer to make my queries as SQL-like as possible. So for example, instead of DB::table('something')->where('column', 'value')->get(), I use

When making changes, the git diff also looks nicer.

intipontt
Автор

It' good to split query logic in such way. For example, you can do any checks before get method. It's fexible, however eloquent always add new functionality and I realy don't know if there is any benefits with such syntax[?]. Anyway developers are different, someone like raw querys, some chained... I think there better look for security. Safe functionality s the right. Thanx for video, , JT

jura
Автор

Thanks, ill start to use it more often

mykola-rohoza
Автор

The main reason to use this method to return an instance from builder if u dont use it the model class will use magice methde __callStatic for example when use where without query methode the parent class model will use methode __callstatic this methde used when u call a static methode not found it this class it will return an instance from builder class and will call where methde from the builder i think this is the main reason from this and like u talk as builder pattern to build the query with chain tech

mahmoudbassam
Автор

I have a suggestion that would help us new developers
Who about a new series each week where you discuss developing a software product from scratch, gathering requirements, gap analysis... Etc to testing the product i know its too much work

Or maby developing a package and publishing it

mohammedabdalla
Автор

I use ->query() every time I want show my intent to filter the database. Besides, it's a plus for readability.

Автор

My personal rule is using query() except when I use ::select() as first method. Then the query itself reads as good as with an opening query().

stefankrejci
Автор

I have a offtopic question:
What is the best way to count all CRUD operations on the specific table?

Observer is a good way, but it doesn't work until mass actions on the table.

pathtic
Автор

If you are adding a with() do you still use the query() or is the with enough to format it nicer for you?

JustinByrne
Автор

I use it so I can ctrl + click through the other methods on the query

obikwelukyrian