Thread Contributor: Baredee[TUT] 5 - Migrations & Schemas - Laravel Tutorial Series
#1
If you didn't catch the last tutorial, you can view it here

~~~~ Hey, everyone! ~~~~


I wonder how many people made it this far. Welcome to your 5th tutorial.

In the last tutorial, we set up a controller, we created an endpoint for the browser (GET request) - we initialised an index
function, and made it return a View that we created with basic Bootstrap.

Now, we're going to be creating a database, a table for our users, and a Model to start utilising Laravel's Eloquent ORM.

What's an ORM?

Stands for Object-relational mapping - it's basically a way to convert data from one form to another accessible one.

So for example, think of serialisation - when working with APIs...

We take JSON/XML data, and we convert them into JavaScript objects.

In the same way, Laravel's Eloquent will take records from our database, and put them into objects! :D

Let's get started...





Make a model


Open artisan (make sure you're in ROOT!) and run "php artisan make:model Person"

I realise the syntax of certain names will be confusing due to the plural form of "person" that
we've chosen being "people" instead of "persons" - but let's work with it.


Run the command.

[Image: 3610daeb3cf643fba34b401fd7aafcd7.png]


Check the directory for Models (/vtech/app)
...there's our model.

(/vtech/app/Person.php)

[Image: dacd32d195ed45fc9fffc1ae5e0b4ce5.png]





Setting up our database


Assuming you've got mysql running somehow (check previous tutorials otherwise) - create a database named vtech.

(http://localhost/phpmyadmin)

[Image: fe24931ff73840be8f5d1ae4627af69a.png]


Why is it http://localhost without :8000?

Because I'm running my database on WAMP, which runs localhost on port 80, and 80 doesn't need to be specified because
the browser recognises that it is the port for HTTP traffic, which is what we're using when we navigate to these URLs.

Our Apache server, however, is running via the "php artisan serve" command that we executed, which should still
be running in a terminal somewhere in your workspace, that one is responsible for our Laravel back-end.

Anyway, time to create a table with the appropriate fields...

Run the following command: "php artisan make:migration "

[Image: 8f9c05db464e4e8a8baf74dc727b091d.png]


Navigate to migrations and search for it (vtech/database/migrations/)

(vtech/database/migrations/[some_timestamp]_create_people_table.php)

[Image: 88920f0087e54995becba31031aaa8a1.png]


Now, you see this file?

In the up() function is what's going to be executed when we run our command to actually
migrate this into our real database (basically we'll design the schema of our table here, and then we'll run a command which will
make Laravel create that table with its fields in our actual database)

Let's create the schema for our table, then.


[Image: ae2aab4dc7064de5a1b064a3f164225f.png]


Might be a little "whoa" - but don't worry.

All we've done is: use some class called Schema, call a static function called create, pass in our desired table-name,
send in a function in the 2nd argument with a blueprint (ignore that)

Just focus on the fields...

We've created an Increments (auto-incrementing ID)
two strings for name and email,
and some timestamps - always keep this.

Now, let's run the command to execute it (to create the table, etc.)

"php artisan migrate"

[Image: 5b5fd6ef7e874a2e891a05afb1e9b5c7.png]


Oh, no.

This is because we haven't configured our database details with our app, let's do that.

(vtech/config/database.php -> line:42)

[Image: cd6ea6a60c4740f3b6147a8e00f4bb28.png]


Currently the mysql driver details start at line 42 in the mentioned file, it may change in the future, but you know what to do.

Put in your details, in the second argument, not the first (got me once)

DB:"vtech"; DB_USR:"root"; DB_PASSWD :"''"

Those are my details.

Next, go to your .env file (should be in root) and change the details there, too

[Image: 1bb91d4b4381496db05cb98941e9c46c.png]


Now try to run the command again.

"php artisan migrate"

[Image: d1a4e633a26446b99b703a1ee9e6b6d1.png]


It says it was successful, and if we check our database we do find two tables...


[Image: 8a5cd511f51b4f539aef8dccb57229a5.png]


However, we don't see our People table.

Now...

For some reason, Laravel does that. I'm not entirely sure why and I haven't really looked into it,
primarily because there's a fix that I always apply straight away.

You can tell it's a limitation issue with string/input length etc. Don't worry about it too much.

Shout out to this guy:


[Image: f6a6af3c906b4afeb952bfaf1cd40f7e.png]


Let's do what he said.

(vtech/app/Providers/AppServiceProvider.php)

[Image: 46a01fcadcab4d51a1cb9de72baa7dcc.png]



Run the command for the third time.


[Image: 5b2901a1adf14f149f647b56c77cf4c2.png]


WTHH?!!?!

This happens because Artisan tries to migrate the two tables that it has already migrated, in addition to the one we want to migrate.

I went through this process on purpose for you to understand the flow of design in Laravel.

To rectify this, create a new folder within migrations/ called done, and keep the migration files for
tables that have already been migrated in there. Leave the [some_timestamp]_create_people_table.php alone in the migrations folder.

Meaning once we're done with this migration, move the file into the done/ folder to join the others, leaving the root of migrations
empty.

Run the command one last time:

"php artisan migrate"

[Image: fcda87597388463abd4095a04fcbe429.png]


Says it was successful. Let's check our database.


[Image: c12ebff2f7b546df9f165de267901b20.png]


:O :O :O

Let's see the structure of it


[Image: a6f563d441cf49b7a3064c554a6047ac.png]


The two fields i've highlighted are a result of the timestamp that I told you to always keep, it benefits you.

Don't remove it unless you know what you're doing.

Cool, right?





Quick Recap


We've created an endpoint for http://ourwebsite/people ...
we've made the Route forward GET requests for this endpoint to a controller named PeopleController,
specifically to the index() function.

The index() function then returns a View that we created, which has a form that's supposed to send a POST request
to our ourwbesite/people endpoint, which we then want to call a create() function which will insert the person as a record
into our database.

Speaking of which, we've created a database, we made a migration and configured its fields within a Schema construction.

We ran into some errors but dealt with them, and finally saw our created database with the correct fields.

Also, we've created a Model which we haven't worked with yet, but that's what will do in the next tutorial.






Conclusion


I don't want to make this tutorial too long, thanks for reading. The steps are pretty self-explanatory.
In the next one we'll POST the form to a new endpoint, and we'll work on creating "persons" :)

If you have any questions, as usual, post in the comment section below and I'll try to get back to you.

Peace.







[Image: a701e0ca783f4720a2616a0d57f28e3b.png]


Possibly Related Threads…
Thread Author Replies Views Last Post
[TUT] 7 - Updating & Deleting / Wrapping up - Laravel Tutorial Series Baredee 6 5,206 05-12-2017, 10:59 AM
Last Post: Baredee
[TUT] 6 - Form->Database - Laravel Tutorial Series Baredee 6 4,727 03-30-2017, 12:00 AM
Last Post: Baredee
[TUT] 4 - Models & Controllers - Laravel Tutorial Series Baredee 0 1,361 02-28-2017, 06:28 PM
Last Post: Baredee
[TUT] 3 - Artisan / Routes / Blade - Laravel Tutorial Series Baredee 0 1,278 02-27-2017, 03:31 PM
Last Post: Baredee
[TUT] 2 - Installation - Laravel Tutorial Series Baredee 0 1,459 02-27-2017, 03:28 PM
Last Post: Baredee

Forum Jump:


Users browsing this thread: 1 Guest(s)