How to Make a Model in Laravel 5.1

Access all tutorials in sprocket icon.

August 14, 2015 from author Bill Keck.

How to Make a Model in Laravel 5.1

In order to follow along with this tutorial, you will have to have already done the install laravel tutorial and the database setup tutorial.

So we’re going to tackle something really basic by making a Widget model that we can use for future tutorials. I’m calling it widget so it can describe anything generic. This will come in really handy and you will love how easy and intuitive creating a model is in Laravel 5.1*.

Let’s start by going to the command line and running:



php artisan make:model Widget -m

Obviously we are using the make command in artisan and you can see how intuitive this really is. You’ll note that we added the flag -m, so that it will also make a migration for us. This is a super optimized workflow, gotta love that.

In your app folder, you will now see a Widget.php file:



<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Widget extends Model
{
   //
}

And that’s all there is to that!

So let’s go to our migration, which is located in database/migrations, you’ll see something like:



2015_08_171507_create_widgets_table.php

The date time is prepended to the descriptive name of the migration. You should note that Laravel expects a singular name for the model and plural name for the actual DB table.

This is very intuitive because you can think of the model returning a single instance of the tables records, so when we want a widget, we look in the widgets table.

Ok, so with migrations, you a nice stub, ready for you:



use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateWidgetsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('widgets', function (Blueprint $table) {
           $table->increments('id');
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::drop('widgets');
   }
}

Obviously you are getting the auto-incrementing id column as well as the timestamps, so this is a great timesaver.

So we’re just going to add one column to the up method to keep this simple:



$table->string('widget_name')->unique();

You’ll note that I’m calling it widget_name instead of name. The reason I like to do this is to keep the column name clear. Imagine you have 10 different models, all with a name column. This makes it harder to work with when you are doing joins, so I take the extra step to prepend the modelname_ to the column name. This is not a hard-baked rule, but I like to follow this convention.

Also note, that we put a unique constraint on it. So now your up method should look like this:



public function up()
{
   Schema::create('widgets', function (Blueprint $table) {
       $table->increments('id');
       $table->string('widget_name')->unique();
       $table->timestamps();
   });
}

Now let’s run the migration from the command line:



php artisan migrate

And with that you should have your new table.

Since we’re planning to add records to the widgets table, we need to add the following property to our Widget model:



/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['widget_name'];

This tells eloquent that this is a mass assignable property, and if we didn’t have this, Laravel’s Eloquent wouldn’t allow us to save any records.

And that’s all there is to making a model in Laravel. In our next tutorial, we will use Laravel’s model factory to populate some test records. Then, in other tutorials, we will make the routes, controllers and views, so we can play with our widgets.

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.

I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5* For Beginners, I really appreciate it.

2 thoughts on “How to Make a Model in Laravel 5.1

  1. Anuj@Anuj-PC MINGW64 /c/wamp/www/demoProject
    $ php artisan make:model User -m
    ←[32mModel created successfully.←[39m

    ←[37;41m
    ←[39;49m
    ←[37;41m [ErrorException]
    ←[39;49m
    ←[37;41m include(C:\wamp\www\demoProject/database/migrations/2014_10_12_000000_create_users_table
    php): ←[39;49m
    ←[37;41m failed to open stream: No such file or directory
    ←[39;49m
    ←[37;41m
    ←[39;49m
    help me out with this

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s