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

michaelnguyen547's avatar

eloquent find vs where/get

I found out that find is more relaxing if the column does not exist. Where/get will give QueryException. Is the find behavior expected?

// tid column does not exit
App\User::find(['tid' => 1]); -> return Illuminate\Database\Eloquent\ Collection

App\User::where(['tid' => 1])->get(); -> throw QueryException 

0 likes
2 replies
Vilfago's avatar

find() will always retrieve data per primary key (define in the model). The key on your array is not used, and is not intended to be a column name.

So yes you get data, but maybe not the data you wanted.

2 likes
jlrdw's avatar

eloquent find vs where/get

There's no such thing, eloquent is a shortcut, so find is a shortcut.

If you browse Builder.php in vendor, find breaks down to:

public function find($id, $columns = ['*'])
    {
        return $this->where('id', '=', $id)->first($columns);
    // more

But if you dig deeper all shortcuts resort back to normal sql pdo statements. The key is is Taylor has made many shortcuts, via active record.

He put a lot of work in this.

I have used eloquent for some things, but as the queries get real complex, it's actually better to use normal queries.

Some okay reading on it:

https://www.yiiframework.com/wiki/2541/when-to-use-active-record

https://laracasts.com/discuss/channels/eloquent/writing-all-queries-directly-vs-model-relations

https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder

https://laracasts.com/discuss/channels/laravel/coverting-ms-access-queries-to-laravel-query-builder

https://laracasts.com/discuss/channels/general-discussion/sql-injection-2

1 like

Please or to participate in this conversation.