Challenge 2: Write Five Query Builder Queries — Possible Solution ==================================================================== use App\Models\Book; // 1. All in-stock books, ordered by price ascending $inStockByPrice = Book::where('in_stock', true)->orderBy('price')->get(); // 2. Books under $15 $cheapBooks = Book::where('price', '<', 15)->get(); // 3. The single book with id=3 (throws if missing) $bookThree = Book::findOrFail(3); // 4. All books excluding out-of-stock ones $availableBooks = Book::where('in_stock', '!=', false)->get(); // equivalently: Book::whereNot('in_stock', false)->get(); // 5. The count of all books $totalBooks = Book::count(); WHY THIS WORKS -------------- - Book::where('in_stock', true)->orderBy('price')->get() chains three method calls, but Eloquent (like Django's QuerySet) only actually runs the SQL once the query is built and terminated with something like ->get() — the same lazy-until-evaluated principle Django's Course 1 covered for QuerySets applies to Eloquent's query builder too. - Book::where('price', '<', 15) uses the three-argument form of where(): column, operator, value — Eloquent supports comparison operators directly as the second argument, rather than Django's __lt-style double-underscore suffix on the field name. - Book::findOrFail(3) is appropriate here specifically because a primary key lookup is guaranteed to match at most one row — mirroring exactly when Django's course recommended .get() as safe: a primary-key-based fetch, not an arbitrary field filter that could plausibly match zero or several rows. - Book::count() runs a SQL COUNT(*) query directly, rather than fetching every row into a PHP collection and counting them in memory — the same performance reasoning Django's .count() QuerySet method follows.