LARAVEL Laravel 3 — Questions and Answers
Question 1: In Laravel's Blade templating engine, which directive is used to yield content from a child view into a parent layout?
- @yield (Correct answer)
- @section
- @include
- @extends
Correct answer: @yield
`@yield('name')` in a parent layout outputs the content defined with `@section('name')` in any child view that `@extends` the layout.
Question 2: What does the Laravel `withTrashed()` Eloquent scope do?
- Includes soft-deleted records in query results (Correct answer)
- Permanently deletes all trashed records
- Restores soft-deleted records
- Filters results to only soft-deleted records
Correct answer: Includes soft-deleted records in query results
When a model uses `SoftDeletes`, calling `withTrashed()` removes the automatic `WHERE deleted_at IS NULL` constraint so soft-deleted rows are included.
Question 3: Which of the following correctly defines a one-to-many relationship in an Eloquent model?
- public function posts() { return $this->hasMany(Post::class); } (Correct answer)
- public function posts() { return $this->belongsTo(Post::class); }
- public function posts() { return $this->hasOne(Post::class); }
- public function posts() { return $this->manyToMany(Post::class); }
Correct answer: public function posts() { return $this->hasMany(Post::class); }
`hasMany()` defines the 'one' side of a one-to-many relationship, meaning one model owns many related model instances.
Question 4: What is the role of Laravel's service container?
- Manage class dependencies and perform dependency injection (Correct answer)
- Store cached service responses
- Handle HTTP service requests and responses
- Register external API service credentials
Correct answer: Manage class dependencies and perform dependency injection
The Laravel service container is a powerful IoC container that resolves class dependencies automatically, enabling constructor and method injection throughout the framework.
Question 5: In Laravel queues, what is the purpose of the `--tries` option in `php artisan queue:work`?
- Sets the maximum number of times a job may be attempted before it fails (Correct answer)
- Sets how many jobs are processed simultaneously
- Limits the number of worker processes
- Defines how long to wait before retrying a failed job
Correct answer: Sets the maximum number of times a job may be attempted before it fails
The `--tries` option specifies how many total attempts are made on a job before it is marked as failed and moved to the failed jobs table.
Question 6: Which Eloquent method retrieves all records as a collection without chunking?
- get() (Correct answer)
- all()
- fetch()
- collect()
Correct answer: get()
Both `get()` and `all()` retrieve all matching records, but `get()` works on query builder chains while `all()` is a static shortcut with no applied constraints.
Question 7: What does the `QUEUE_CONNECTION` environment variable control in Laravel?
- Which queue driver (sync, database, redis, sqs, etc.) is used to process jobs (Correct answer)
- The database connection used to store queue credentials
- The number of queue worker processes to spawn
- The default job retry delay in seconds
Correct answer: Which queue driver (sync, database, redis, sqs, etc.) is used to process jobs
`QUEUE_CONNECTION` selects the active queue backend driver as defined in `config/queue.php`, such as `sync` for immediate execution or `redis` for async processing.
In Laravel's Blade templating engine, which directive is used to yield content from a child view into a parent layout?