In this project, you will build a simple blog application using Laravel. The project will include user authentication and CRUD functionality (Create, Read, Update, Delete) for managing blog posts.
Lesson Outline
- Setting Up the Environment
- Building User Authentication
- Creating the Blog Post Model, Migration, and Controller
- Implementing CRUD Functionality
- Securing the Application
- Enhancing the Blog with Styling and UX
Step 1: Setting Up the Environment
1.1 Install Laravel
Follow the steps from Lesson 19 to install Laravel:
- Install Laravel using Composer:
- Navigate to the project directory:
1.2 Configure the Database
- Create a new MySQL database named
blog
. - Update the
.env
file with your database credentials: - Run the migration command to ensure Laravel’s default migrations are applied:
Step 2: Building User Authentication
Laravel provides a built-in authentication system, which you can scaffold using the Laravel Breeze package.
2.1 Install Laravel Breeze
- Install Breeze:
- Scaffold the authentication system:
- Install the frontend dependencies:
- Run the migration command to create the necessary tables for authentication:
2.2 Test Authentication
- Start the Laravel server:
- Open your browser and navigate to
http://localhost:8000
. - Register a new user and log in to verify that the authentication system works.
Step 3: Creating the Blog Post Model, Migration, and Controller
3.1 Create a Blog Post Model and Migration
- Run the Artisan command to generate the model and migration:
- Open the migration file in
database/migrations/
and define the schema: - Run the migration:
3.2 Create a Controller for Posts
Generate a resource controller:
The PostController
will handle CRUD operations for blog posts.
Step 4: Implementing CRUD Functionality
4.1 Define Routes
Open routes/web.php
and define the routes for the Post resource:
4.2 Add Methods in the PostController
Show All Posts (Index)
Show Form to Create a Post
Store a New Post
Edit an Existing Post
Update a Post
Delete a Post
4.3 Create Blade Templates
List All Posts (posts/index.blade.php
)
Create Post Form (posts/create.blade.php
)
Edit Post Form (posts/edit.blade.php
)
Step 5: Securing the Application
- Protect Routes:
- Ensure routes are protected with the
auth
middleware. - Use
abort(403)
to prevent unauthorized access to posts.
- Ensure routes are protected with the
- Validation:
- Validate user input to prevent invalid or malicious data.
Activities and Exercises
- Enhance the Blog:
- Add categories to posts.
- Allow posts to be marked as “published” or “draft.”
- Styling:
- Use Bootstrap or Tailwind CSS to style the blog.
- Search:
- Implement a search feature to find posts by title or content.
Assignment
- Extend the blog application to include:
- Image uploads for posts.
- Pagination for the list of posts.
- A public page where non-authenticated users can view published posts.
- Write a test case to ensure only authenticated users can create, edit, or delete posts.
Summary
In this project, you:
- Built a blog application with Laravel.
- Implemented user authentication.
- Added CRUD functionality for blog posts.
- Secured routes and user permissions.
This project demonstrates the power of Laravel for building real-world applications. Let me know if you’d like to explore advanced features or additional enhancements!
Leave a Reply