Projects with Laravel
Back to Blog
Web Development & Programming 5 min read

Projects with Laravel

In this article
Laravel is a PHP web application framework with an expressive and elegant syntax. It has an extensive library of packages and takes care of much of the heavy lifting of programming, leaving you free to focus on your creativity.
One creative use of Laravel is building a personal blog. This tutorial describes how to use Laravel to build and publish a blog on Kinsta.
For a preview of the project, check out the complete project code.

Laravel features and functionality

In addition to the functionality mentioned above, Laravel has more features that make web development easier and faster. Some of these features are:

  • Routing: Laravel provides a simple and clean interface for routing. You can define the routes for your application using a simple and expressive syntax.
  • Controllers: In Laravel, controllers provide an easy way to organize the logic for your application.
  • Views: Laravel's Blade templating language provides an easy way to create views for your application.
  • Models: Laravel's Eloquent ORM makes it easy to work with databases in your application.
  • Migrations: Laravel migrations provide a way to easily update your database schema.
  • Artisan CLI: Laravel provides a command-line interface (CLI) called Artisan that gives you many useful commands for your application.

Prerequisites

  • A web server. 
  • A GitHub, GitLab or Bitbucket account to publish your application's code.
  • Laravel installed. 

Make sure the Apache and MySQL module services are running.

Create a New Laravel Project

  1. Go to your machine's terminal or command-line interface (CLI).
  2. Create a Laravel project called blog using the command laravel new blog.
  3. Open your project's blog directory with the command cd blog.
  4. Next, open the directory in your code editor.
  5. To check that you have built the project correctly, run php artisan serve in your terminal or CMD.
  6. Click the local address output to view it in the browser. The browser should display the default Laravel welcome page, shown below:

Página de Bienvenida de Laravel cuando se sirve al navegador

Set up the database

Create and configure the database by going back to phpMyAdmin in your browser and creating a database called blog.

  1. To create the database, in the Databases tab, type «blog» in the Create database field.
  2. Then click Create.

Creación de base de datos en el panel phpMyAdmin

  1. Next, update the database connection in your .env file at the root of your blog project. Change the DB_DATABASE and DB_PASSWORD values to the ones you created.

The connection details should look like this.

The other database connection details remain the same as in the .env file. If you change any connection value, such as changing DB_PORT from 3306 to 3307 during setup, make sure to update it in the .env file.

 

Creating Migrations

Migrations in Laravel are about designing the tables of our database, and although we could create our table directly in MySQL and then create the controller and the model, we are going to do it using migrations. What is the difference? The difference is that with migrations you design the tables as if they were models and then you can generate them in MySQL with a single command. You can also keep a chronology of the creation of your tables, and if something went wrong, for example you forgot to create a field, you can easily do a rollback and undo the changes. But to make it clearer, let's go to the example.

The table we are going to create for the example will be called Artists and will contain the fields: name, description. To create the table we create a migration file. To do this, we go to the command window opened earlier and use the following command:

 

You will notice that a new file has now been created inside the database->migratios folder
2018_01_26_035203_create_artists_table.php.

It is a class that inherits from the Migration class. In this class we define the fields the Artists table will contain. This class has two methods: dow, which is called when we run a rollback, and up, which is where we will create the fields for our table. The file should look as shown below:

Create_Table_Artists

Now all that is left is to run the migration so that our artists table is created. To do this, we run the following command:

All that remains is to check the artists table. For this you can use phpMyadmin, or if you use MySQL Workbeanch you can see that the libros table was indeed created along with other additional ones.

As I mentioned, in certain cases you may want to add a field to a table that has already been created. You can simply do a rollback, which in this case deletes the tables created with the migrations, leaving the database empty without any tables. You can try the following command and see what happens:

If you check the database again, you will notice that it has returned to its initial state, and since this command was only for testing, you must run the migrate command again to create the artists table.

Enjoyed the article? Share it!

Related Articles

Keep exploring similar content.

Contact Us

We're ready to take your project to the next level. Get in touch and let's talk about your vision.

Address

Machala - Ecuador

Email

* Required fields