PHP Laravel Fun: How to use Laravel with a MySQL database: Inventory Management System MVC case study
#software #halifax #softwaredeveloper #php #laravel
This article will show you how Laravel works and how you can create an inventory management system that connects to a MySQL database over a webserver.
This was one of our final projects in my IT - Programming at Nova Scotia Community College - NSCC
In simple terms. Laravel is an opensource PHP framework for web development. Laravel used MVC the structure and allows you to handle models, views and controllers to interact with database, routing and caching.
The point of the project is to make a system that allows you to create categories and items with edit/deletes which are held on a MySQL server.
Lets start
Step one - DOWNLOAD BASICS
Get the following basics. XAMPP as it gives you PHP and also MySQL. You also need Composer, MySQL Workbench and 7zip for unpacking the Laravel dependencies.
Step two - MAKE LARAVEL PROJECT
Run these commands to set up your Laravel project folder.
After you make the working folder, CD into it and run this command
php artisan serve
This will spin up the Laravel webserver and allow you to access your project on 127.0.0.1:8000
Step Three - MAKE DATABASE
Set up your database.
You'll need to start your XAMPP MySQL server. You also need to write your migration files. In Laravel, migration files are just a function that builds tables and columns inside your MySQL database. So once you have this code to build the tables. then you run the command below.
php artisan make:migration create_items_table
In this example Categories needs to be made first because it has a category_id which is used as a foreign key in the Items table.
But for this example I'll just show the items table first.
Once you have the code for the tables. You need to run this command:
php artisan migrate
This will run the code and build the tables into your database which you can see of MySQL Workbench.
So now you have the tables built inside your database. You ran the migration command which takes the code in your migration files and builds the tables.
Step Four - Build the Model, View and Controller Logic
So in Laravel you have a system architecture that uses MVC within the framework. Model View Controller structure helps developers produce content on the web easier and assists with hooking everything up so that we can get API routes to call different functions within our project. (That is a very limiting description that is very basic from my understanding so far lol)
Controllers
Above you can see that the ItemContoller.php file dictates what happens with the business logic when the user is adding a new item, deleting a new item, editing an item, storing the item in the database etc...
The controller class for item stores the useable functions for items essentially.
Controllers act as the middle logic between the Model and the View, which we see on our browser.
For example, when we create an item, the store function dictates what happens when we actually create the item on the browser. Inside item::create we can see that the different column names are used to request data from the input and sets the input to the column name inside of our MySQL database.
Model
The above is the model for item. This essentially is a class for item which dictates what it is. It has these columns in the database.
领英推荐
Views
Once we start adding categories and items, we will see the data in our database. The view part of MVC dictates how it displays on the browser. So above we see that the table is built with php. The name of these views will be index.blade.php for example. Blade is a type of markdown I would assume that allows us to see the html based code on the browser.
Routes
The last puzzle piece to get all of this to work is the web.php file.
This file holds the Laravel routing. Routes are basically what you see in the url bar of your browser.
For example www.youtube.com/justinbishop
This youtube link uses a route of /justinbishop to take you to my Youtube page.
These are routes.
When a user accesses a URL, Laravel's routing system directs the request to the appropriate controller method, allowing you to organize and handle your application's logic effectively.
So the routes bind everything together in the Model View Controller MVC concept.
Final Product
So that was basically how you use Laravel. All of that labor allows you to make a 1990's looking database system on your browser.
So amazing right? Welcome to the future.
The future is now.
Like always, thanks for reading my interpretation of PHP Laravel and how to hook all of this up so we can add and delete from a MySQL database from the comfort of your own browser!
Share if you found this useful or entertaining.
Thanks,
Justin Bishop
Send me a message if you need help.
Follow me here on Linkedin for software development articles Justin Bishop
#software #halifax #softwaredevelopment #php #laravel #webdevelopment #sql
Dalhousie Faculty of Computer Science