Concept Flow - Select queries
Start
Build Query
Add Select Columns
Execute Query
Fetch Results
Use Data
End
This flow shows how Laravel builds and runs a select query step-by-step, from starting the query to getting results.
use Illuminate\Support\Facades\DB; $users = DB::table('users') ->select('id', 'name') ->where('active', 1) ->get();
| Step | Action | Query State | Result/Output |
|---|---|---|---|
| 1 | Start query builder for 'users' table | Query builder initialized for 'users' | No output yet |
| 2 | Add select columns 'id', 'name' | Select columns set to ['id', 'name'] | No output yet |
| 3 | Add where condition 'active' = 1 | Select columns set to ['id', 'name'] and where active = 1 | No output yet |
| 4 | Execute get() to fetch results | Final SQL: SELECT id, name FROM users WHERE active = 1 | Query sent to database |
| 5 | Fetch results from database | Results received as collection of user records | Collection of users with id and name where active=1 |
| 6 | Use data in application | Data ready for use | Users data accessible in $users variable |
| Variable | Start | After Step 2 | After Step 3 | After Step 5 | Final |
|---|---|---|---|---|---|
| $users | undefined | undefined | undefined | Collection of user records | Collection of user records |
Laravel Select Queries:
- Start with DB::table('table_name')
- Chain ->select('col1', 'col2') to pick columns
- Add ->where('col', value) for conditions
- Call ->get() to run query and fetch results
- Without select(), all columns (*) are fetched by default