Simplicity is the essence of happiness. — Cedric Bledsoe

Mentioning this quote has a reason, I found this quote in my terminal. You will later know how I have found it. This quote clearly represents the popular PHP framework Laravel. Maybe you are a beginner (know a little bit of PHP and have some OOP concept) back-end developer or a little bit experienced developer wants to test Laravel by doing a small project? Then keep scrolling!

Prerequisites

Basic command-line experience

PHP ≥7.2, MySQL, Composer, Apache 2 installed on your machine

Basic OOP concept

Visual Studio Code or PhpStorm or your preferable IDE

Project Overview

User (you, me or other !) can c reate a story (just like in Medium)

reate a story (just like in Medium) C an show all stories on a page

an show all stories on a page S how a specific story

how a specific story U pdate any story

pdate any story Delete any story

A little concept for making things crystal clear!

I don’t know if you heard about MVC or not! MVC is an acronym for ‘Model View Controller’. It represents the architecture, developers adopt when building applications. Laravel implements this pattern so well. What the MVC pattern actually is? Okay, let’s break it part by part -

The Model contains only pure application data.

contains only pure application data. The View presents the model’s data to the user.

presents the model’s data to the user. The Controller exists between the view and the model. It handles the requests from routes.

Just think of this web application (Medium.com) the different pages of stories you see are provided by the View layer. When you click on a particular story to view more you actually send a request through a Route, the Controller layer processes your action. It handles your request and gets the specific data you need from a data source using the Model layer. The data is then bundled up together and arranged in a View layer and displayed to you. Rinse and repeat.

Story-Book — MVC architecture in the diagram

Just keep this tiny little concept in your head it will help you later. We will move forward step by step.

Step-1: Installing Laravel and settings things up

Make sure you have all the prerequisites in your local machine. Then run the command in terminal.

composer create-project --prefer-dist laravel/laravel story-book "5.8.*"

Be patience, this will take some moments…After finishing installation import the story-book directory to your preferable IDE. Don’t get overwhelmed by all the files and directories under the story-book directory, things will be easy and interesting after reading this article!

Now move to story-book directory from terminal and generate the application key

cd story-book php artisan key:generate

This is called the artisan command. You will be introduced with more artisan commands step by step, these commands really help you and reduce your headache by automating some of our tasks.

Now create a MySQL database for our story-book app.

$mysql -u root -p (your password here)

$mysql>CREATE DATABASE storybook_db;

Create a file named .env and copy all the contents from .env.example and paste into .env file. This file contains all the environment variables that our app depends on these. Put the database name and credentials for our app so our .env file will look like this —

APP_NAME=Laravel

APP_ENV=local

APP_KEY=*your generated app key here*

APP_DEBUG=true

APP_URL=http://localhost LOG_CHANNEL=stack DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=story_book_db

DB_USERNAME=root

DB_PASSWORD= *your db password here *

Step-2: Creating a model and making migrations

Okay, we are good to go. Can you remember the little concept we left behind?. First of all, we need a Story model that represents data about the story. Let’s make it with the artisan command

php artisan make:model Story -m

-m option stands for its migration file that will be created or not. The above-mentioned command will create two things, one a Story model in the app directory and another one is a migration file in

database/migrations/xxx_xx_xx_xxxxxx_create_stories_table.php

Our story has a title and a body, nothing else.

migration_for_create_stories_table

If you take a look at the migration file you will see an up() function. The up() function is used for creating/updating tables, columns, and indexes. The down() function is used for reversing an operation done by up() method.

Now we will make a migration by the following command

php artisan migrate

This command will create the stories table in our story_book_db database. We have to add the fillable properties in our newly created model. It’ll be like this

Mass assignment in Story model

Step-2: Creating StoryController to handle user interactions with model

We have our Story model and now we need a controller for it. Let’s make it with the artisan command

php artisan make:controller StoryController

Okay you have found our newly created controller in

app/Http/Controllers/StoryController.php

We have got our Model and Controller, now focus on our project objectives and implement them one by one.

Step-3: Create a story and view all stories on the homepage

Let’s open up the web.php file located in app/routes directory. Remove or comment out the default route

//Route::get('/', function () {

// return view('welcome');

//});

And add our own route like this

Story Book — Route for view all stories

You can see that the route has a common structure. These are -

→HTTP Request method

→A URI

→A controller method responsible for handling request

→Route name

So we need a method in our StoryController too. Let’s add the method

Story Book — Index method in StoryController for viewing all stories

This tiny method will fetch all the stories from the database and send them in our view section. Okay, now you may say then where is SQL query here! My answer is

Laravel has come up with a better solution for database query and it is Eloquent ORM. It will make your life easier. You don’t have to write SQL queries over and over again. Eloquent ORM has some pretty methods to handle all these things. You just have to maintain the proper relationships among your Models and Eloquent will do the rest.