Basic Laravel Questions
1. What is Laravel?
Laravel is a free, open-source PHP framework designed to make web application development easier by providing built-in features like routing, authentication, and database management. It follows the MVC (Model-View-Controller) architectural pattern.
2. What are the key features of Laravel?
- Eloquent ORM: Simplifies database operations.
- Blade Template Engine: Helps with templating.
- Routing System: Manages URLs efficiently.
- Middleware: Adds security layers.
- Authentication & Authorization: Inbuilt user authentication.
- Queues & Jobs: Handles background tasks.
3. How does Laravel compare with other PHP frameworks like CodeIgniter and Symfony?
- CodeIgniter: Lighter but lacks built-in authentication and ORM.
- Symfony: More powerful but has a steeper learning curve.
- Laravel: Balanced, feature-rich, and easier to learn.
4. What is Composer in Laravel?
Composer is a dependency manager for PHP that allows you to install and manage Laravel packages efficiently.
5. What is Artisan in Laravel?
Artisan is Laravel’s built-in command-line tool for performing tasks like migrations, queue management, and controller generation.
6. What is the latest version of Laravel?
You can check the latest version at Laravel's official site.
7. What are Service Providers in Laravel?
Service Providers manage the bootstrapping of Laravel applications by binding services into the Service Container.
8. Explain Middleware in Laravel.
Middleware filters HTTP requests and can be used for authentication, logging, and request modifications before reaching controllers.
9. How does Laravel handle routing?
Routes are defined in routes/web.php
(for web) and routes/api.php
(for APIs) using methods like Route::get()
, Route::post()
, etc.
10. What is CSRF protection in Laravel?
Laravel uses CSRF tokens (@csrf
directive in Blade) to prevent cross-site request forgery attacks.
Database and Eloquent ORM
11. What is Eloquent in Laravel?
Eloquent is Laravel’s ORM (Object-Relational Mapping) that simplifies database interactions using models.
12. What are migrations in Laravel?
Migrations are version control for databases, allowing schema changes via PHP code instead of SQL.
13. How do you create a migration in Laravel?
Run:
php artisan make:migration create_users_table
Then, define schema changes inside database/migrations/
.
14. What is the difference between hasOne()
and belongsTo()
relationships?
- hasOne(): A user has one profile.
- belongsTo(): A profile belongs to a user.
15. What is a pivot table in Laravel?
A pivot table handles many-to-many relationships without a primary key column.
16. How do you define many-to-many relationships in Laravel?
Using belongsToMany()
in both models.
17. What is the difference between up()
and down()
methods in migration files?
up()
: Defines schema changes.down()
: Reverts them.
18. How do you use query scopes in Laravel?
Define a scope in a model:
public function scopeActive($query) {
return $query->where('status', 'active');
}
Use it as:
User::active()->get();
19. How do you perform raw SQL queries in Laravel?
Using DB::select()
or DB::statement()
.
20. What are database seeders in Laravel?
Seeders populate tables with test data using php artisan db:seed
.
Authentication & Authorization
21. How does Laravel handle authentication?
Laravel provides built-in authentication scaffolding using php artisan make:auth
(Laravel 6 and below) or Laravel Breeze/Fortify for newer versions.
22. What is Passport in Laravel?
A package for OAuth2 API authentication.
23. What is Laravel Sanctum?
A lightweight API authentication system for single-page applications (SPAs) and mobile apps.
24. How do you define roles and permissions in Laravel?
Using Spatie’s Laravel Permission package.
25. How do you customize Laravel’s authentication logic?
By modifying the LoginController
or using custom guards.
Blade Templating & Views
31. What is Blade in Laravel?
Blade is Laravel’s templating engine that allows dynamic content rendering.
32. How do you pass data to a view in Laravel?
Using return view('welcome', ['name' => 'John']);
33. How do you include one Blade view inside another?
Using @include('header')
.
34. What are Blade components?
Reusable UI components, defined in resources/views/components/
.
35. What is the difference between @yield
and @section
in Blade?
@yield
: Placeholder for content.@section
: Defines content for a section.
Requests & Validation
41. How does Laravel handle form validation?
Using request()->validate()
.
42. How do you create a Form Request in Laravel?
Run:
php artisan make:request StoreUserRequest
Define validation rules in rules()
.
43. What is the difference between validate()
and validated()
methods?
validate()
: Stops execution if validation fails.validated()
: Returns validated data.
Queues & Events
71. What is Laravel Queues?
Queues handle background tasks asynchronously using queue drivers like Redis or database.
72. How do you create a queue job in Laravel?
Run:
php artisan make:job SendEmailJob
Dispatch using:
dispatch(new SendEmailJob($user));
Testing & Debugging
91. How does Laravel handle testing?
Using PHPUnit and Laravel Dusk for browser testing.
92. What is PHPUnit in Laravel?
PHPUnit is a testing framework used for writing unit and feature tests.
93. How do you create and run tests in Laravel?
Run:
php artisan make:test ExampleTest
php artisan test
94. What is Laravel Dusk?
A browser testing tool for testing UI interactions.
No comments:
Post a Comment