LARAVEL Laravel 4 — Questions and Answers
Question 1: What is Laravel Sanctum primarily used for?
- Issuing API tokens for SPAs and mobile applications without OAuth complexity (Correct answer)
- Full OAuth2 server implementation with authorization codes
- Social login via third-party providers like GitHub and Google
- Two-factor authentication via SMS
Correct answer: Issuing API tokens for SPAs and mobile applications without OAuth complexity
Laravel Sanctum provides a lightweight token-based authentication system ideal for SPAs and simple token-based APIs, without the full overhead of OAuth2.
Question 2: In Laravel, what is an 'accessor' on an Eloquent model?
- A method that transforms a model attribute value when it is retrieved (Correct answer)
- A method that validates data before saving to the database
- A middleware that controls access to model resources
- A way to define computed columns in a database migration
Correct answer: A method that transforms a model attribute value when it is retrieved
Accessors are defined using the `Attribute::make()` method (Laravel 9+) or a `get{AttributeName}Attribute()` method to transform values when they are read from a model.
Question 3: Which command generates a form request class for validation in Laravel?
- php artisan make:request StoreUserRequest (Correct answer)
- php artisan make:form StoreUserRequest
- php artisan make:validation StoreUserRequest
- php artisan generate:request StoreUserRequest
Correct answer: php artisan make:request StoreUserRequest
`php artisan make:request` creates a class in `app/Http/Requests` with `authorize()` and `rules()` methods for clean, reusable validation logic.
Question 4: What is the purpose of the `up()` method in a Laravel migration file?
- Defines the database changes to apply when the migration runs (Correct answer)
- Rolls back the migration changes
- Seeds the database with initial records
- Validates the migration schema before running
Correct answer: Defines the database changes to apply when the migration runs
The `up()` method contains the forward migration logic (creating tables, adding columns), while `down()` defines the reversal used by `php artisan migrate:rollback`.
Question 5: In Laravel's route model binding, what happens when a bound model is not found by default?
- Laravel automatically returns a 404 HTTP response (Correct answer)
- Laravel returns null and continues execution
- Laravel throws an Eloquent ModelNotFoundException that must be manually caught
- Laravel redirects to the home route
Correct answer: Laravel automatically returns a 404 HTTP response
Implicit route model binding automatically returns a 404 response when the model is not found, without requiring any additional code in the controller.
Question 6: What does the `compact()` PHP function do when used in a Laravel controller returning a view?
- Creates an associative array from variable names and their current values in scope (Correct answer)
- Compresses the view output for faster delivery
- Combines multiple view files into one
- Merges request data with session data
Correct answer: Creates an associative array from variable names and their current values in scope
`compact('user', 'posts')` is equivalent to `['user' => $user, 'posts' => $posts]`, offering a concise way to pass variables to views.
Question 7: Which Laravel feature allows you to schedule Artisan commands and closures to run at defined intervals?
- Task Scheduling via the Console Kernel (Correct answer)
- Laravel Horizon
- Queue workers with delay
- Laravel Forge cron management
Correct answer: Task Scheduling via the Console Kernel
Laravel's Task Scheduler, configured in `App\Console\Kernel::schedule()`, lets you define recurring tasks using a fluent API and requires only a single cron entry on the server.
What is Laravel Sanctum primarily used for?