Prepare for Your Laravel Interview: Basic, Intermediate, and Advanced Questions
Mastering Laravel interview questions is essential for developers targeting roles at companies like Zoho, Paytm, Salesforce, and Atlassian. This comprehensive guide covers 30 essential Laravel questions categorized by difficulty level, perfect for freshers, developers with 1-3 years experience, and senior professionals with 3-6 years of expertise.
Basic Laravel Interview Questions (1-10)
1. What is Laravel and what are its key features?
Laravel is a PHP web framework following the MVC architecture. Key features include Eloquent ORM, Blade templating engine, robust routing system, Artisan CLI, middleware support, and built-in authentication[1][4].
2. What are the different route files in Laravel?
Laravel provides two main route files: web.php for web routes with session and CSRF protection, and api.php for API routes with rate limiting and stateless authentication[4].
3. Explain MVC architecture in Laravel.
MVC divides applications into Model (data logic), View (user interface), and Controller (request handling). Laravel follows this pattern for better code organization and maintainability[4][6].
4. What is the Artisan command to create a model?
Use php artisan make:model User to generate a new Eloquent model class for database interactions[4].
5. What is Blade templating engine?
Blade is Laravel’s templating engine with clean syntax for views. It supports directives like @if, @foreach, components, and inheritance using @extends and @section[1][4].
6. How do you define a basic route in Laravel?
Route::get('/welcome', function () {
return 'Hello Laravel!';
});
7. What are Laravel migrations?
Migrations are PHP classes that manage database schema changes using php artisan make:migration. They provide version control for database structure[3][4].
8. What is the purpose of the app/Http/Kernel.php file?
The HTTP Kernel defines global middleware, route middleware, and middleware groups that filter HTTP requests and responses[1].
9. How do you create a controller in Laravel?
Use php artisan make:controller UserController to generate a controller class for handling HTTP requests[4].
10. What is route model binding?
Route::get('user/{user}', function (App\Models\User $user) {
return $user;
});
Laravel automatically resolves the model instance from the route parameter[3].
Intermediate Laravel Interview Questions (11-20)
11. What are Eloquent relationships? Name the basic types.
Eloquent supports one-to-one (hasOne, belongsTo), one-to-many (hasMany, belongsToMany), and many-to-many relationships defined in model classes[3].
12. Explain middleware in Laravel.
Middleware filters HTTP requests entering or leaving the application. Register in Kernel.php as global, route-specific, or groups[1][7].
13. How do you implement validation in Laravel?
Use Form Requests or Validator facade with rules like 'required', 'email', 'min:6'. Create with php artisan make:request StoreUserRequest[3].
14. What are Laravel queues and how do you use them?
Queues handle time-consuming tasks asynchronously. Configure drivers like database, Redis, then dispatch jobs using dispatch(new SendEmailJob())[1][6].
15. How do you perform database queries using DB facade?
use Illuminate\Support\Facades\DB;
$users = DB::table('users')->where('votes', '>', 100)->get();
16. What is eager loading and why is it important?
Eager loading prevents N+1 query problem using with(): User::with('posts')->get() loads relationships in single query[2].
17. Explain named routes in Laravel.
Route::get('/user/profile', [UserController::class, 'profile'])->name('user.profile');
Generate URLs using route('user.profile')[6].
18. What are Laravel events and listeners?
Events trigger actions like user registration. Define events and listeners, then fire with event(new UserRegistered($user))[6].
19. How do you create API resources in Laravel?
Use php artisan make:resource UserResource to transform Eloquent models to JSON responses for APIs[2][3].
20. What is the difference between delete() and forceDelete() in Eloquent?
delete() uses soft deletes if trait enabled; forceDelete() permanently removes records[1].
Advanced Laravel Interview Questions (21-30)
21. Explain hasOneThrough vs hasManyThrough relationships.
hasOneThrough defines one-to-one across intermediates; hasManyThrough defines one-to-many. Example: Country → User → Post[2].
22. How do you implement polymorphic relationships?
Polymorphic allows one model to belong to multiple parent types using morphTo(), morphMany() with commentable_id and commentable_type[5].
23. What are query scopes in Eloquent?
public function scopePopular($query) {
return $query->where('votes', '>', 100);
}
Post::popular()->get();
24. How do you cache queries in Laravel for Paytm-scale applications?
Use Cache::remember('users', 3600, function () { return User::all(); }); with Redis/Memcached and tags for group invalidation[2].
25. Explain Laravel Sanctum vs Passport.
Sanctum provides API tokens and SPA authentication; Passport implements full OAuth2 server for complex API scenarios at Salesforce-scale[3].
26. How do you optimize Laravel for high traffic like Swiggy?
Implement route caching (php artisan route:cache), config caching, Redis queues, database query optimization, and Octane for request handling[2].
27. What are accessors and mutators in Eloquent?
public function getFullNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
Accessors format retrieval; mutators format setting[1].
28. How do you implement rate limiting in Laravel APIs?
Route::middleware('throttle:60,1')->group(function () {
Route::get('/user', [UserController::class, 'index']);
});
Limits 60 requests per minute[3].
29. What is Laravel Octane and when to use it?
Octane serves requests using Swoole/ RoadRunner for 3x performance improvement in high-throughput applications like Zoho[2].
30. How do you handle database transactions in Laravel?
DB::transaction(function () {
$user = User::create([...]);
$profile = $user->profile()->create([...]);
});
Ensures atomic operations with rollback on failure[3].
These 30 Laravel interview questions cover essential concepts for technical interviews at product companies. Practice coding examples and understand real-world scenarios for success.