MVC in Laravel – Part 2

Still talking about routes

Behind every request has a pre-defined route for it action.

Laravel uses the HTTP Verbs in your routes, however by default HTML forms do not support PUT, PATCH or DELETE actions. To get around you will need to add a hidden _method field to the form.

Let’s see the default actions of a resource controller

Back to our default example

We will create a simple page with news from database to demonstrate how it works. In future we will see more about each topic of this first example, remember it’s only to demonstrates how is MVC in Laravel. Don’t focuses on little details because we will review each topic with more details.

Artisan the power of console

Some tasks in development are repetitive and boring, it’s cool have I friend in these times, yes we have, it’s artisan, the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Type php artisan in your application root directory to see a lot of commands that you can do with it.

See above a piece of output.

Database Schemas

To use databases, of course we need to configure it before start.

Do you remember the config directory? Yes, we need to go there to understand how Laravel uses the .env configuration file to made some changes.

As will you see, are many configuration files there. To start, open the app.php and look the variables that you maybe like to change.

Look the env(), Laravel uses PHP Dotenv to get environment variables (https://github.com/vlucas/phpdotenv)

It means that APP_NAME will be searched in your .env file, otherwise, if don’t find it will be put Laravel as default. I advise you to give a little break  to look your /config/database.php before made the needed changes in our .env file.

Here’s the default .env file

If you are not familiar with Homestead yet, don’t worry, in this example we will use SQLite and php built-in server.

Let’s do some changes in our .env file

Firstly we can do little changes
APP_NAME to NewsExample
DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite

our final .env file will be like below:

To create the database.sqlite use the touch command ( for mac )

Everything done? Go next!

First look at Migrations, why I love it!

Laravel offer to us a excellent way to maintain and control our database schemas, say hello to Migrations.

At the first view, I have to confess that I thought that it was not necessary because I always had used the MySQL WorkBench or SQL Power Architect (You still can use it together) but work in team with GIT we can see the benefits of this feature.

Simple imagine how we can maintain all our machines updated, including databases, and is the best reason that I had focused in this feature. Of course exists many other things that made it great.

To start our project, we need to create a new migration of news table, we can use Artisan to do it.

If you goes to database directory, will see:

Well, artisan did for us the migration table and put it on migrations dir.

Here’s the migration

I don’t talk much about the Schema facade at this time, we will only do the migration to this example, but note that we have a function up to run on artisan migrate command and the function down() that run when you need to rollback the schema.

Well as we used the param –create=users the artisan generates the blueprint in up() to create a table with increments(‘id’) and timestamps ( created_at and updated_at ).

Let’s add the title and body columns to our schema:

After this changes, we need to update our database

All the table’s are now created by artisan, use the DB Browser to explore your database: http://sqlitebrowser.org/ get the correct version for your OS.

Now we have the table, but not yet a model, let’s create it!

the result, app/News.php

Use the DB Browser to populate it example with some news.

In the next post we will finalize this introduction to MVC.

See you soon 🙂

Share this!




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *

*