Tự học Laravel: (9) CREATE A MODEL

Last time , I used Migration to create the Articles table. This time, I will create a model of the MVC pattern , and manipulate DB data through the model.

In addition, Laravel names the function to associate DB with model object as Eloquent. The class is a class that inherits the functions of Eloquent and adds business logic.

CREATE MODEL

Create a model using artisan. 
Although table names are named in the plural form (Articles), it is customary to name model names in the singular form (Article). Please be careful.

php artisan make:model Article 

Model files are created directly under the app directory.

app 
└── Article.php
<?php //app/Article.php
namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Article extends Model {
    //
}

An empty class was created just by inheriting Eloquent’s Model. It’s kind of strange, but you can manipulate the Articles table with this one.

REVIEW OF THE SCHEMA OF THE ARTICLES TABLE

Before you go a model operation of, the last leave to review the schema of the Articles table that you created.

CREATE TABLE "articles" (
    "id" integer not null primary key autoincrement,
    "title" varchar not null,
    "body" text not null,
    "published_at" datetime null,
    "created_at" datetime not null,
    "updated_at" datetime not null
);

The id is automatically numbered by the database. 
created_at, updated_at are called timestamps, and are automatically set by Eloquent. 
title, body, published_at are items set by the user.

MODEL OPERATION

Try working with the Article model using tinker. 
tinker is a tool for interacting with the Laravel application interactively. It is very useful when you want to try a little movement.

Now run tinker.

php artisan tinker

Here are the operations on tinker.

CREATE AND SAVE ARTICLE

>>> $article = new App\Article()
=> App\Article {# 2861}
>>> 
>>> $ article->toArray()
=> []
>>> 
>>> $ article->title = 'First time title'
=> "First time title"
>>> 
>>> $ article->body = 'Something'
=> "Sorry"
>>> 
>>> $ article-> published_at = Carbon\Carbon::now()
=> Carbon\Carbon @ 1535050359 {# 2864
     date: 2018-08-24 03: 52: 39.156431 Asia/Tokyo (+09: 00),
   }
>>> 
>>> $ article->toArray()
=> [
       "title" => "First time title",
       "body" => "That's it",
       "published_at" => Carbon\Carbon @ 1535050359 {# 2864
           date: 2018-08-24 03: 52: 39.156431 Asia/Tokyo (+09: 00),
       },
   ]
>>> 
>>> $ article->save()
=> true
>>> 
  • Create an empty Article in the $article variable by adding a new Article class.
  • Set values ​​for title, body, and published_at.
  • Set the current time obtained by Carbon class to published_at.
  • Save data to DB with save().

I could set values ​​in the Article class even though there were no member variables or accessor methods (geter, setter) defined. 
In fact, Eloquent uses PHP magic methods __set () and __get () to enable access to member variables corresponding to table items.

VIEW ALL ARTICLE

>>> App\Article::all()->toArray()
=> [
     [
       "id" => 1,
       "title" => "First time title",
       "body" => "That's it",
       "created_at" => "2018-08-24 03:54:26",
       "updated_at" => "2018-08-24 03:54:26",
       "published_at" => "2018-08-24 03:52:39",
     ],
   ]
>>> 
>>> exit 

All data of Article table was acquired by all() of Article class. You can confirm that only one data has been added.

As it has become longer, the operation of the model is temporarily suspended here. Continued next time.

SUMMARY

You can do the following things

  • Create model
  • Create model instance and save to DB
  • Get all table data from DB

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.