LARAVEL Professional Standards & Competencies 3 — Questions and Answers
Question 1: What is the professional standard for writing Laravel feature tests that interact with the database?
- Use the production database with transactions
- Use the `RefreshDatabase` or `DatabaseTransactions` trait to reset state between tests (Correct answer)
- Manually truncate tables after each test
- Mock all Eloquent models
Correct answer: Use the `RefreshDatabase` or `DatabaseTransactions` trait to reset state between tests
The `RefreshDatabase` trait runs migrations fresh per test suite and `DatabaseTransactions` wraps each test in a rolled-back transaction to ensure isolation.
Question 2: Which approach is the Laravel-recommended way to authorize actions in controllers?
- Write if/else checks manually in every controller method
- Use Gate facades or Policy classes with `authorize()` (Correct answer)
- Check user roles directly via `Auth::user()->role`
- Use middleware only for all authorization logic
Correct answer: Use Gate facades or Policy classes with `authorize()`
Laravel Policies and Gates provide a clean, centralized authorization layer called via `$this->authorize()` in controllers.
Question 3: What does it mean to follow the 'thin controller, fat model' principle in Laravel professionally?
- Controllers should have many lines of SQL; models should be minimal
- Business logic resides in models/services, while controllers only handle HTTP input/output (Correct answer)
- Models should contain routing logic
- Controllers should call the database directly without models
Correct answer: Business logic resides in models/services, while controllers only handle HTTP input/output
Thin controllers delegate business logic to models or service classes, making controllers responsible only for receiving requests and returning responses.
Question 4: Which Laravel tool is the professional standard for managing database schema changes across a team?
- Directly editing the database schema via phpMyAdmin
- Using Laravel Migrations with version-controlled migration files (Correct answer)
- Exporting and importing SQL dumps
- Using Eloquent to create tables dynamically
Correct answer: Using Laravel Migrations with version-controlled migration files
Migrations provide version-controlled, team-shareable database schema changes that can be applied or rolled back consistently.
Question 5: What is the recommended Laravel practice for handling complex query logic that is reused across multiple controllers?
- Duplicate the query in each controller
- Define named scopes on the Eloquent model or use a Repository class (Correct answer)
- Store queries in a config file
- Write a database view and query it directly
Correct answer: Define named scopes on the Eloquent model or use a Repository class
Eloquent local scopes encapsulate reusable query constraints on the model, while Repositories centralize more complex data-access patterns.
Question 6: In professional Laravel projects, where should form validation logic live?
- Directly in the Blade view with JavaScript only
- In dedicated Form Request classes (Correct answer)
- As anonymous closures in routes/web.php
- Inside the Eloquent model's boot method
Correct answer: In dedicated Form Request classes
Form Request classes encapsulate validation rules and authorization, keeping controllers clean and making validation logic reusable and testable.
Question 7: Which Laravel feature should professionals use to send consistent JSON error responses for API projects?
- Manually catch every exception and return response()->json()
- Customize the exception handler in `bootstrap/app.php` or `App\Exceptions\Handler` (Correct answer)
- Return HTML error pages with a 200 status code
- Disable exception handling entirely
Correct answer: Customize the exception handler in `bootstrap/app.php` or `App\Exceptions\Handler`
Registering renderable exceptions or customizing the Handler's `render` method provides a centralized place to return consistent API error responses.
What is the professional standard for writing Laravel feature tests that interact with the database?