Tự học Laravel: (10) ELOQUENT

Last time , I created an Article model and used Eloquent’s functions to manipulate the model. We will continue to try Eloquent’s features.

REVIEW OF THE SCHEMA OF THE ARTICLES TABLE

Before working with the model, review the schema of the Articles table.

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
);

MODEL OPERATION

Try working with the Article model using tinker.

php artisan tinker

Here are the operations on tinker.

ADD ONE MORE DATA

>>> $article = new App\Article
>>> $ article-> title = '2nd article'
>>> $ article-> body = 'Bringe'
>>> $article->published_at = Carbon\Carbon::now()
>>> $article->save()
>>> App\Article::all()->toArray()
=> [
       [
           "id"           => "1",
           "title" => "First time title",
           "body" => "That's it",
           "created_at"   => "2015-02-22 21:08:16",
           "updated_at"   => "2015-02-22 21:08:16",
           "published_at" => "2015-02-22 20:47:07"
       ],
       [
           "id"           => "2",
           "title" => "2 items",
           "body"         => "ほげほげ",
           "created_at"   => "2015-02-23 07:35:46",
           "updated_at"   => "2015-02-23 07:35:46",
           "published_at" => "2015-02-23 07:35:35"
       ]
   ]
>>> 

GET RECORDS BY ID

>>> App\Article::find(1)->toArray()
=> [
       "id"           => "1",
       "title" => "First time title",
       "body" => "That's it",
       "created_at"   => "2015-02-22 21:08:16",
       "updated_at"   => "2015-02-22 21:08:16",
       "published_at" => "2015-02-22 20:47:07"
   ]
>>> App\Article::find(2)->toArray()
=> [
       "id"           => "2",
       "title" => "2 items",
       "body"         => "ほげほげ",
       "created_at"   => "2015-02-23 07:35:46",
       "updated_at"   => "2015-02-23 07:35:46",
       "published_at" => "2015-02-23 07:35:35"
   ]

GET RECORD BY ID OR THROW EXCEPTION

>>> App\Article::findOrFail(2)->toArray();
=> [
       "id"           => "2",
       "title" => "2 items",
       "body"         => "ほげほげ",
       "created_at"   => "2015-02-23 07:35:46",
       "updated_at"   => "2015-02-23 07:35:46",
       "published_at" => "2015-02-23 07:35:35"
   ]
>>> App\Article::findOrFail(3)->toArray();
Illuminate\Database\Eloquent\ModelNotFoundException with message 'No query results for model [App\Article].'

SPECIFY SEARCH CONDITIONS AND EXTRACT

>>> App\Article::where('id', '>', 0)->get()->toArray();
=> [
       [
           "id"           => "1",
           "title" => "First time title",
           "body" => "That's it",
           "created_at"   => "2015-02-22 21:08:16",
           "updated_at"   => "2015-02-22 21:08:16",
           "published_at" => "2015-02-22 20:47:07"
       ],
       [
           "id"           => "2",
           "title" => "2 items",
           "body"         => "ほげほげ",
           "created_at"   => "2015-02-23 07:35:46",
           "updated_at"   => "2015-02-23 07:35:46",
           "published_at" => "2015-02-23 07:35:35"
       ]
   ]
>>> App\Article::where('id', '>', 1)->get()->toArray();
=> [
       [
           "id"           => "2",
           "title" => "2 items",
           "body"         => "ほげほげ",
           "created_at"   => "2015-02-23 07:35:46",
           "updated_at"   => "2015-02-23 07:35:46",
           "published_at" => "2015-02-23 07:35:35"
       ]
   ] 

AGGREGATE

>>> App\Article::count();
=> 2
>>> App\Article::where('id', '>', 1)->count();
=> 1

UPDATE

>>> $article = App\Article::find(2);
=>  {
       id: "2",
       title: "2nd article",
       body: "hoge hoge",
       created_at: "2015-06-18 07:54:55",
       updated_at: "2015-06-18 07:54:55",
       published_at: "2015-06-18 07:54:51"
   }
>>> $ article-> body = 'Fugofuga';
=> "Fugofaga"
>>> $article->save();
=> true
>>> App\Article::find(2)->toArray();
=> [
       "id"           => "2",
       "title" => "2 items",
       "body"         => "ふがふが",
       "created_at"   => "2015-02-23 07:35:46",
       "updated_at"   => "2015-02-23 08:04:05",
       "published_at" => "2015-02-23 07:35:35"
   ]

DELETE

>>> $article = App\Article::find(2);
=>  {
       incrementing: true,
       timestamps: true,
       exists: true,
       snakeAttributes: true,
       manyMethods: [
           "belongsToMany",
           "morphToMany",
           "morphedByMany"
       ]
   }
>>> $article->delete();
>>> App\Article::all()->toArray();
=> [
       [
           "id"           => "1",
           "title" => "First time title",
           "body" => "That's it",
           "created_at"   => "2015-02-22 21:08:16",
           "updated_at"   => "2015-02-22 21:08:16",
           "published_at" => "2015-02-22 20:47:07"
       ]
   ]

SUMMARY

You can do the following things.

  • Using Eloquent
  • Get records by id
  • Get records by id or throw an exception
  • Extract by specifying search criteria
  • Summarization
  • Update
  • Delete

Eloquent has many other features and mechanisms. It is hard to remember all at once, so I would like to take a look at the official site and check again when needed. 
http://laravel.com/docs/master/eloquent

Leave a Reply

Your email address will not be published.

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