My Journey with Laravel: Building My First Project
Introduction:
Last week, I embarked on a journey to create my first Laravel project for a web module, despite being unfamiliar with the Laravel framework. What's remarkable is that I completed this compact project in under 4 hours. Laravel, a powerful PHP framework, became my trusted companion on this fast-paced adventure.
Setting up
A brief description of the installation process and tools used for Laravel on macOS:
Installation Process on macOS
The installation procedure is quite simple and can be found at this link: https://laravel.com/docs/10.x/installation.
1. Install Homebrew (optional): While not mandatory, Homebrew is a popular package manager that can simplify the installation of certain dependencies. You can install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
2. Install Composer: Composer is a dependency management tool for PHP that Laravel relies on. You can install Composer using Homebrew or download it directly from the [Composer website](https://getcomposer.org/download/).
brew install composer
3.Install Laravel Installer: Laravel provides a convenient installer that simplifies project creation. You can install it globally using Composer.
composer global require laravel/installer
4.Open Terminal: Launch the Terminal application on your macOS. Navigate to the htdocs Directory: Use the cd (change directory) command to navigate to your htdocs directory. For example:
cd /path/to/htdocs
Replace /path/to/htdocs with the actual path to your htdocs directory.
5.Create Your Laravel Project: Once you're in the htdocs directory, you can use the Laravel Installer to create a new Laravel project. For instance:
laravel new your-laravel-project
Replace "your-laravel-project" with the desired name for your Laravel project.
By creating your Laravel project in the htdocs directory, it's set up to be served by your local Apache web server, making it easy to develop and test your web application locally.
5. Serve Your Application: After creating the project, navigate to its directory and start a development server.
cd project-name
php artisan serve
That's it! You've successfully installed Laravel on macOS and created your first Laravel project. You can access your application in a web browser at `http://localhost:8000`.
In my project, as you can see the laravel project is found in the htdocs folder and XAMPP is running in the background.
Authentification
1. Database Connection:
I configured the database connection in the `.env` file, specifying the database host, port, name, username, and password.
2. AuthController and HomeController:
I created an `AuthController` and a `HomeController` to handle user authentication and serve the homepage for authenticated users.
3. Blade Views:
I designed and created Blade views for key pages such as the login, registration, and home pages. These views were styled using Bootstrap 5 for a polished user interface.
4. Routing Logic:
In the `routes/web.php` file, I defined routes for various user actions, including registration, login, and access to the homepage. These routes were organized based on the user's authentication status (guest or authenticated). Use middleware to protect routes that require authentication, ensuring only authorized users can access them.
Register page
Home Page
CRUD (Create, Read, Update, Delete) Operations
CRUD operations are fundamental actions in data management systems that allow users to Create new records, Read existing data, Update records with new information, and Delete records when they are no longer needed. These operations form the backbone of database interactions in software applications.
Product Controller:
I created a ProductController with resourceful routes using the Artisan command `php artisan make:controller ProductController --resource`. This controller handles all the CRUD operations for products.
Database Migration:
I generated a database migration using `php artisan make:model Product -m` to create a migration file for the products table. This migration defines the table schema with fields like title, price, product_code, description, and timestamps.
Model:
I defined the Product model in `app/Models/Product.php`, specifying the fillable attributes to allow mass assignment.
Blade Views:
I designed Blade views for creating, editing, listing, and displaying product details, ensuring a user-friendly interface. These views extend a common layout and utilize Bootstrap 5 for styling.
Routing:
In the `routes/web.php` file, I used resourceful routing to define routes for handling products, including index, create, store, show, edit, update, and destroy actions. These routes map to corresponding controller methods.
By sharing these implementation details of the Product management system within my Laravel project, I aim to provide a valuable reference for others looking to build similar CRUD functionality in their applications.
The product page
Pagination
Pagination is a web technique that divides large datasets into manageable pages, improving user experience and content navigation. Laravel offer tools to implement pagination effectively in web applications.
Create Model and Migration:
I used the Artisan command `php artisan make:model Post -m` to create a new model named Post along with a migration file. The model is located in the app directory (`app/Models/Post.php`), while the migration file is found in the `database/migrations` directory. The migration file defines the structure of the "posts" table in the database, including fields like title, slug, body, and timestamps.
Model (app/Models/Post.php):
I defined the Post model, specifying its attributes and indicating that it uses the HasFactory trait. The fillable attributes are set to 'title', 'body', and 'slug'.
Migration (database/migrations/create_posts_table.php):
The migration file `create_posts_table.php` contains the database schema for the "posts" table. It includes fields such as 'title', 'slug', 'body', and timestamps for tracking creation and modification times.
Database Migration:
After creating the migration file, I executed `php artisan migrate` to apply the migration and create the required table in the database.
Create Factory Class:
I generated a factory class named `PostFactory` using the command `php artisan make:factory PostFactory --model=Post`. This factory is used to generate test data for the "posts" table.
Execute Factory:
I used the Laravel Tinker tool (`php artisan tinker`) to execute the factory and generate 50 test records for the "posts" table.
Create Controller:
I created a controller named `PostController` using the command `php artisan make:controller PostController`. This controller is responsible for handling requests related to posts.
Create View File (resources/views/posts.blade.php):
I designed a Blade view file for displaying posts. The view uses Bootstrap 5 for styling and paginates the posts. It includes a loop to iterate through the posts and display their titles and bodies in a card layout.
Add Bootstrap Paginator:
I added Bootstrap pagination to the project by modifying the `AppServiceProvider.php` file to use Bootstrap paginator.
Routing (routes/web.php):
I defined a route for displaying posts using the `PostController`'s `index` method. This route is accessible at the 'posts' URL.
This summary outlines the steps taken to create a model, migration, factory, controller, view, and routing for managing posts in a Laravel project, including the incorporation of Bootstrap for styling and pagination.
Conclusion
In summary, this blog post demonstrated how Laravel empowers developers to swiftly implement authentication, pagination, and CRUD operations. These essential features are seamlessly integrated into our Laravel project, enhancing user management, user experience, and data handling. With Laravel's efficiency and robustness, you're well-prepared to tackle web development projects effectively. Happy coding with Laravel! I also want to mention that the Laravel documentation and Laracast have been invaluable resources in my journey, providing crucial guidance and insights. For those interested in exploring the code, you can access the project on https://github.com/SultaanMooraby/laravel.

Comments
Post a Comment