Learn Laravel from Scratch [FULL BOOTCAMP COURSE]
Table of Contents
Introduction
In this tutorial, you will learn how to build a full-stack application using Laravel by following a comprehensive bootcamp course. The project we'll create is a social media application called Chirper, which allows users to share "Chirps" to a feed. This guide will walk you through the entire process from setting up your Laravel project to deploying your application, making it perfect for beginners and those looking to deepen their understanding of Laravel's capabilities.
Step 1: Setting Up Your Laravel Project
-
Install Laravel:
- Make sure you have Composer installed on your machine.
- Open your terminal and run the following command to create a new Laravel project:
laravel new chirper
-
Navigate to Your Project Directory:
- Change to the newly created project directory:
cd chirper
- Change to the newly created project directory:
-
Serve the Application:
- Start the Laravel development server by running:
php artisan serve - Access your application at
http://localhost:8000.
- Start the Laravel development server by running:
Step 2: Creating Your First Route
-
Open Routes File:
- Navigate to the
routes/web.phpfile in your project.
- Navigate to the
-
Define a Route:
- Add a simple route that returns a welcome view:
Route::get('/', function () { return view('welcome'); });
- Add a simple route that returns a welcome view:
-
Test Your Route:
- Visit
http://localhost:8000in your browser to see the welcome page.
- Visit
Step 3: Understanding MVC Architecture
-
Learn About MVC:
- MVC stands for Model-View-Controller, a design pattern that separates your application into three main components:
- Model: Handles data logic.
- View: Manages the presentation layer.
- Controller: Acts as an intermediary between Model and View.
- MVC stands for Model-View-Controller, a design pattern that separates your application into three main components:
-
Real-World Application:
- Use MVC to organize your code effectively, making it easier to maintain and scale your application.
Step 4: Working with the Database
-
Set Up Database Configuration:
- Configure your database settings in the
.envfile (e.g., updateDB_DATABASE,DB_USERNAME, andDB_PASSWORD).
- Configure your database settings in the
-
Run Migrations:
- Create your database tables using Laravel migrations:
php artisan migrate
- Create your database tables using Laravel migrations:
Step 5: Creating Your First Model
-
Generate a Model:
- Create a model for Chirps using:
php artisan make:model Chirp -m - This command also creates a migration file.
- Create a model for Chirps using:
-
Define the Chirp Schema:
- Open the migration file in
database/migrationsand define the schema for your Chirps table.
- Open the migration file in
-
Run the Migration:
- Execute the migration to create the table:
php artisan migrate
- Execute the migration to create the table:
Step 6: Showing the Feed
-
Create a Feed View:
- Create a view file in
resources/viewscalledfeed.blade.php.
- Create a view file in
-
Fetch Chirps in a Controller:
- Generate a controller:
php artisan make:controller ChirpController - In your controller, fetch all Chirps and pass them to the feed view.
- Generate a controller:
Step 7: Creating and Storing Chirps
-
Create a Form for Chirps:
- In your feed view, add a form to submit new Chirps.
-
Store Chirps in the Database:
- In your
ChirpController, create a method to handle form submission and store Chirps in the database.
- In your
Step 8: Edit and Delete Chirps
-
Implement Edit Functionality:
- Create routes and methods in your controller to edit existing Chirps.
-
Implement Delete Functionality:
- Add functionality to delete Chirps by creating a route and method in your controller.
Step 9: Implementing Basic Authentication
-
Set Up Authentication:
- Use Laravel's built-in authentication scaffolding:
composer require laravel/ui php artisan ui vue --auth npm install && npm run dev
- Use Laravel's built-in authentication scaffolding:
-
Test Registration and Login:
- Access the registration and login routes to test user authentication.
Conclusion
By following this tutorial, you have laid the groundwork for your first Laravel application, Chirper. You've learned to set up a project, create routes, work with the database, and implement basic authentication. As the next steps, consider expanding your application with more features, exploring Laravel's extensive documentation, or diving into advanced topics like API development or testing. Happy coding!