Your first Laravel application - Database first

Database First vs Code First

MVC frameworks genrally allow the programmer to either auto-generate the Classes from the SQL tables or to auto-generate the SQL tables from the classes. This is called scaffolding. The debate rages as to whether its better to start with a "code first" or a "database first" approach. I am an unashamed database-first developer. For me, the database (or other data persistence mechanism) is the foundations of the house. Get it right and everything will flow happily from there, get it wrong and it's likely the system will never work.

Many frameworks include the ability to scaffold out of the box. Others provide a range of different add-ons or plugins which allow you to scaffold. Laravel falls into this latter category. In truth, I would prefer if this was provided as core-functionality out of the box but we can't let that hold us back. Laravel's whole approach is to allow for an eco-system of packages which can be added on. As can readily be seen with WordPress and plugins this can ultimately be a benefit. It just takes a little time to filter through what's available and get a base set of packages you can live with comfortably.

In the Laravel world, scaffolders are referred to as "Generators" or "Admin Panel Generators". I'm not so interested in the Admin Panel aspect of this, more in the ability to generate CRUD - Create, Read, Update and Delete functionality. In more recent times this term is being replaced by BREAD - Browse, Read, Edit, Add, Delete functionality. Ability to automatically generate the Model, View and Controller objects necessary to deliver BREAD functionality is, for me, what makes MVC frameworks so compelling.

Before we start scaffolding we need a database to form the basis of our application. The script below allows you to create a simple three-table database for a Tennis Club. Over the course, we will build a range of functionality around this simple example. Apologies if you're not a Tennis fan.

If you're not that familiar with SQL or relational databases you may struggle a bit with this step. On the other hand it may help you gain a better understanding. This really depends on what point of the curve you are on regarding understanding of databases in general.

Before you can run the following script you need to have your preferred WAMPP stack installed (I generally use XAMPP). You also need a SQL client. I find MySQL workbench a bit to bloated for my taste so I tend to use HeidiSQL (it takes 2 mins to install) but phpMyAdmin will also work fine.

Fire up your MySQL server and connect to it using your MySQL client. The following script will create the database and insert some test data. If for any reason you have a problem, you can run the script again and it will regenerate the database, dropping the previous version.

 drop database if exists tennisclub;
 create database tennisclub;
 use tennisclub;
 create table member(
     id integer auto_increment,
     firstname varchar(30),
     surname varchar(30),
     membertype varchar(6),
     dateofbirth date,
     created_at datetime,
     updated_at datetime,
     deleted_at datetime,
     primary key(id)
 );
 create table court
 (
     id integer auto_increment,
     surface varchar(30),
     floodlights boolean,
     indoor boolean,
     created_at datetime,
     updated_at datetime,
     deleted_at datetime,
     primary key(id)
 );
 create table booking
 (
     id integer auto_increment,
     bookingdate date,
     starttime time,
     endtime time,
     memberid integer,
     courtid integer,
     fee decimal(18,3),
     created_at datetime,
     updated_at datetime,
     deleted_at datetime,
     primary key(id),
     foreign key(memberid) references member(id),
     foreign key(courtid) references court(id)
 );
 insert into member (firstname,surname,membertype,dateofbirth) values ('oliver','kerr','senior','2000-01-31');
 insert into member (firstname,surname,membertype,dateofbirth) values ('morgan','bartlett','senior','1985-10-28');
 insert into member (firstname,surname,membertype,dateofbirth) values ('rebecca','house','senior','1993-08-01');
 insert into member (firstname,surname,membertype,dateofbirth) values ('leslie','hammond','senior','1999-03-03');
 insert into member (firstname,surname,membertype,dateofbirth) values ('axel','gibson','senior','2007-09-29');
 insert into member (firstname,surname,membertype,dateofbirth) values ('bo','bradshaw','senior','1979-12-19');
 insert into court(surface, floodlights, indoor) values('savannah',1,1);
 insert into court(surface, floodlights, indoor) values('grass',1,0);
 insert into court(surface, floodlights, indoor) values('savannah',1,1);
 insert into court(surface, floodlights, indoor) values('savannah',1,1);
 insert into court(surface, floodlights, indoor) values('grass',0,0);
 insert into court(surface, floodlights, indoor) values('grass',0,0);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-01-07','09:00:00','11:00:00',2,3,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-02-11','16:00:00','17:00:00',5,3,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2016-11-16','10:00:00','12:00:00',4,2,20.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-04-06','14:00:00','16:00:00',2,5,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-01-17','17:00:00','18:00:00',5,6,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-05-06','15:00:00','17:00:00',3,6,15.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-04-27','09:00:00','11:00:00',2,3,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2016-05-05','16:00:00','17:00:00',5,3,10.00);
 insert into booking (bookingdate,starttime,endtime,memberid,courtid,fee) values ('2017-03-23','10:00:00','12:00:00',4,2,20.00);
 

Now that the Database has been created we can create our Laravel project

Leave a Reply