📖 Scenario: You are building a simple Laravel application to manage a bookstore. You need to retrieve book data from the database using Laravel's Eloquent ORM.
🎯 Goal: Learn how to write basic select queries using Laravel's Eloquent ORM to fetch data from the books table.
📋 What You'll Learn
Create an Eloquent model for the books table
Write a query to select all books
Write a query to select books with a price greater than 20
Write a query to select only the title and author columns
Write a query to get the first book with the title 'Laravel Basics'
💡 Why This Matters
🌍 Real World
Selecting data from a database is a common task in web applications. Laravel's Eloquent ORM makes it easy to write readable and efficient queries.
💼 Career
Understanding how to write select queries with Eloquent is essential for backend developers working with Laravel to build data-driven applications.
Progress0 / 4 steps
1
Create the Book model
Create an Eloquent model called Book that uses the books table.
Laravel
Hint
Use class Book extends Model and set protected $table = 'books';
2
Select all books
Write a query using the Book model to get all books and assign it to a variable called allBooks.
Laravel
Hint
Use Book::all() to get all records.
3
Select books with price greater than 20
Write a query using the Book model to get books where the price is greater than 20. Assign the result to a variable called expensiveBooks.
Laravel
Hint
Use Book::where('price', '>', 20)->get() to filter books.
4
Select specific columns and get first matching book
Write a query using the Book model to select only the title and author columns. Assign it to titleAuthorBooks. Then write a query to get the first book where the title is 'Laravel Basics' and assign it to laravelBook.
Laravel
Hint
Use select('title', 'author')->get() and where('title', 'Laravel Basics')->first().
Practice
(1/5)
1. What does the select() method do in a Laravel query builder?
easy
A. It specifies which columns to retrieve from the database.
B. It inserts new data into the database.
C. It deletes rows from the database table.
D. It updates existing records in the database.
Solution
Step 1: Understand the purpose of select()
The select() method tells Laravel which columns you want to get from the database table.
Step 2: Compare with other query methods
Methods like insert(), delete(), and update() perform different actions, not selecting columns.
Final Answer:
It specifies which columns to retrieve from the database. -> Option A
Quick Check:
select() chooses columns [OK]
Hint: Remember: select() picks columns, not rows or actions [OK]
Common Mistakes:
Confusing select() with insert() or update()
Thinking select() deletes data
Assuming select() fetches all columns by default
2. Which of the following is the correct syntax to select the columns 'name' and 'email' from the 'users' table using Laravel's query builder?
easy
A. DB::table('users')->select(['name', 'email'])->get();
B. DB::table('users')->select('name' & 'email')->get();
C. DB::table('users')->select('name, email')->get();
D. DB::table('users')->select('name' + 'email')->get();
Solution
Step 1: Check the correct way to pass multiple columns
Laravel's select() accepts an array of column names to select multiple columns.
Step 2: Analyze each option's syntax
DB::table('users')->select('name, email')->get(); uses select('name, email'), which selects a single column named 'name, email'. Options B and D use invalid operators & and +. DB::table('users')->select(['name', 'email'])->get(); correctly uses an array ['name', 'email'].
Final Answer:
DB::table('users')->select(['name', 'email'])->get(); -> Option A
Quick Check:
Use array for multiple columns in select() [OK]
Hint: Use array syntax for multiple columns in select() [OK]
Common Mistakes:
Passing multiple strings without array
Using operators like + or & inside select()
Forgetting to call get() to execute query
3. What will be the output of the following Laravel query?