0
0
Laravelframework~10 mins

Select queries in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Laravel
use Illuminate\Support\Facades\DB;

$users = DB::table('users')
    ->select('id', 'name')
    ->where('active', 1)
    ->get();
This code selects the 'id' and 'name' columns from the 'users' table where 'active' is 1, then fetches the results.
Execution Table
StepActionQuery StateResult/Output
1Start query builder for 'users' tableQuery builder initialized for 'users'No output yet
2Add select columns 'id', 'name'Select columns set to ['id', 'name']No output yet
3Add where condition 'active' = 1Select columns set to ['id', 'name'] and where active = 1No output yet
4Execute get() to fetch resultsFinal SQL: SELECT id, name FROM users WHERE active = 1Query sent to database
5Fetch results from databaseResults received as collection of user recordsCollection of users with id and name where active=1
6Use data in applicationData ready for useUsers data accessible in $users variable
💡 Query executed and results fetched; process ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
$usersundefinedundefinedundefinedCollection of user recordsCollection of user records
Key Moments - 2 Insights
Why do we chain methods like select() and where() instead of writing one big query?
Laravel's query builder uses method chaining to build queries step-by-step, making it easier to read and modify. See execution_table steps 2 and 3 where select and where are added separately before execution.
What does get() do in the query?
The get() method executes the built query and fetches the results from the database. Before get(), no query is run (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the query state after step 3?
ASelect columns set to ['id', 'name'] and where active=1
BQuery builder initialized for 'users' only
CFinal SQL executed
DResults received from database
💡 Hint
Check the 'Query State' column at step 3 in the execution_table.
At which step does the actual database query run?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the step where 'Final SQL' is mentioned in the execution_table.
If we remove the select() method, what will the query select by default?
AOnly 'id' column
BNo columns, query fails
CAll columns (*)
DOnly 'name' column
💡 Hint
Laravel defaults to selecting all columns if select() is not specified.
Concept Snapshot
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
Full Transcript
This visual execution shows how Laravel builds a select query step-by-step. First, the query builder starts with the users table. Then, select columns 'id' and 'name' are added. Next, a where condition filters active users. The get() method runs the SQL query and fetches results. The users data is then ready to use. Variables like $users hold the fetched data after execution. Key points include method chaining for building queries and get() triggering the database call.