PHP & Laravel: Modern Web Development Fundamentals
PHP & Laravel: Modern Web Development Fundamentals
Up to 20 questions, shuffled on every run
Ready to check how well you know PHP and Laravel? This quiz runs through routing and middleware, Eloquent relationships and the N+1 trap, migrations and seeders, Blade, and the artisan workflow. Read the explanations. That is where the learning happens.
Answer key and explanations20 questions
The quiz above draws 20 questions at random from these 26, so a second attempt will not be the same run. Everything in the pool is listed here.
In Laravel, where do you typically define web routes?
Answerroutes/web.php
Web routes live in routes/web.php (with the web middleware group), while API routes live in routes/api.php.
What does route model binding do?
AnswerIt automatically injects the model instance matching a route parameter, resolving it from the database.
A typed parameter like User $user resolves the User whose key matches the route segment, returning a 404 if none is found, so you skip the manual lookup.
What is Eloquent?
AnswerLaravel's ORM that maps database tables to model classes.
Eloquent is an active-record ORM: each model class represents a table and each instance a row, with expressive methods for querying and relationships.
What is the N+1 query problem in Eloquent?
AnswerLoading a list and then lazily querying a relationship for each item, causing one query plus N more.
Accessing a relationship inside a loop fires a query per record. Eager loading with with() fetches the relationship in one extra query instead of N.
How do you eager load a relationship to avoid N+1?
AnswerModel::with(\"comments\")->get()
with() eager loads the named relationship in a single additional query, so accessing it in a loop does not trigger per-row queries.
What does a database migration let you do?
AnswerDefine and version schema changes in code so they can be applied and rolled back consistently.
Migrations are versioned schema definitions with up() and down() methods, so the whole team applies the same changes and can roll back.
Which artisan command runs pending migrations?
Answerphp artisan migrate
php artisan migrate applies any migrations that have not run yet. migrate:rollback reverses the last batch.
What is Blade?
AnswerLaravel's templating engine, compiled to plain PHP and cached.
Blade templates use directives like @if and {{ }} and compile down to cached PHP, so they add expressiveness with little runtime cost.
How does Blade help prevent XSS by default?
AnswerThe {{ }} echo syntax escapes HTML entities automatically.
Blade escapes output in {{ }} automatically. Use {!! !!} only for trusted HTML you explicitly want rendered raw.
What is middleware in Laravel?
AnswerA layer that filters or modifies HTTP requests entering the application, such as authentication.
Middleware wraps requests, so you can check auth, verify CSRF tokens, or rate-limit before the request reaches your controller.
How do you generate a controller with artisan?
Answerphp artisan make:controller UserController
The make: namespace scaffolds classes: make:controller, make:model, make:migration, and so on. Add --resource for RESTful methods.
What does the service container provide?
AnswerDependency injection: it resolves and injects class dependencies automatically.
Laravel's service container resolves classes and their dependencies, so type-hinted constructor or method parameters are injected for you.
What is a service provider?
AnswerA central place to register bindings and bootstrap parts of the application.
Service providers register container bindings and run bootstrapping logic; they are the wiring that assembles the framework and your packages.
Which Eloquent relationship models "a post has many comments"?
AnswerhasMany
A Post defines comments() returning hasMany(Comment::class); the inverse on Comment is belongsTo(Post::class).
Which relationship needs a pivot table?
AnswerbelongsToMany (many-to-many)
A many-to-many relationship (belongsToMany), like users and roles, uses a pivot table holding the two foreign keys.
What protects Laravel forms against CSRF?
AnswerA CSRF token included via @csrf, validated by middleware.
The @csrf directive adds a hidden token to the form, and the VerifyCsrfToken middleware rejects POST requests without a valid one.
What are Eloquent migrations seeded with?
AnswerSeeders and factories generate test/sample data.
Database seeders (often using model factories) populate tables with sample or reference data via php artisan db:seed.
What does Eloquent mass assignment protection prevent?
AnswerSetting model attributes that are not listed as fillable from untrusted input.
The $fillable (or $guarded) property whitelists which attributes can be set via create()/fill(), preventing a user from injecting fields like is_admin.
How do you return JSON from a controller?
Answerreturn response()->json($data) (or just return an Eloquent model/collection)
response()->json() sets the content type and encodes the payload; returning a model or collection directly also serializes to JSON automatically.
What is an API Resource in Laravel?
AnswerA transformation layer that shapes a model into a JSON structure for API responses.
API Resources (JsonResource) let you control exactly which fields and shape a model exposes in an API, decoupling the response from the table.
How does Laravel handle background work?
AnswerQueued jobs processed by workers (php artisan queue:work).
Jobs are pushed to a queue (database, Redis, SQS) and processed asynchronously by a worker, keeping slow work out of the request cycle.
What does php artisan tinker give you?
AnswerAn interactive REPL to run code against your application.
tinker opens a REPL with your app booted, so you can query models, call services, and experiment interactively.
Where are environment-specific settings like DB credentials stored?
AnswerIn the .env file, read via config and the env() helper.
The .env file holds environment values and is not committed; config files read them, and env() should be used inside config, not scattered through the app.
What is the purpose of php artisan config:cache in production?
AnswerIt combines all config into one cached file for faster boot.
Caching config (and routes) avoids parsing many files on every request. Remember to rebuild the cache after changing config in production.
What does the belongsTo side of a relationship indicate?
AnswerThe model holds the foreign key pointing to its parent.
belongsTo is the inverse of hasOne/hasMany and lives on the model that carries the foreign key (e.g. a Comment belongsTo a Post).
How do you validate incoming request data in a controller?
AnswerCall $request->validate([...]) or use a Form Request class.
$request->validate() checks rules and redirects back with errors on failure; a Form Request moves the rules into a dedicated, reusable class.








