Todo Laravel Project - Model - Working with todos table

Laravel Project

Share

In this Laravel project tutorial we will learn to create model using Artisan.

Recap

Till now we have accomplished the following.

  • Created a new todo-laravel project
  • Created a new laravel_todo_db database
  • Created a new todos table using migration command

Eloquent Model

Eloquent is the default ORM (Object Relational Mapper) of Laravel.

For every database table we create a model which helps to interact with the table.

To create a model in Laravel we take help of the make:model command.

So, let's create a model for the todos table by running the following command in the terminal.

$ php artisan make:model Todo

On success we will get the following output.

Model created successfully.

Model naming convention

By convention we name model in singular like Todo and it will interact with the database table have plural name like todos.

So, if your table name is lets say books (in plural) then by convention your model name must be Book (in singular).

The Todo.php model file

On running the make:model command we will get a new file inside the app directory.

Since we named our model Todo so, a new file Todo.php was created inside the app directory with the following code.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Todo extends Model
{
  //
}


In the next tutorial we will play with the todos table using Tinker. It will be awesome.

Thanks for reading. See you in the next tutorial.

Have fun coding :-)

Share