开启 tinker ,进行交互式编程。
$ php artisan tinker
然后,就可以在练习中学会如何操作数据库了,比如:
实例化一个 model,并添加一些数据,这里用到了 Carbon
类提供的操作的时间的方法:
>>> $article = new App\Article();
=> <App\Article #0000000060c3016a000000016bd293a3> {}
>>> $article->title = "标题1"
=> "标题1"
>>> $article->body ="文章内容1"
=> "文章内容1"
>>> $article->published_at = Carbon\Carbon::now();
=> <Carbon\Carbon #0000000060c30169000000016bd2980b> {
date: "2016-11-30 06:25:06.000000",
timezone_type: 3,
timezone: "UTC"
}
可以方便的将刚才的实例转化成各种需要的格式,使用 toArray()
或 toJson()
>>> $article
=> <App\Article #0000000060c3016a000000016bd293a3> {
title: "标题1",
body: "文章内容1",
published_at: <Carbon\Carbon #0000000060c30169000000016bd2980b> {
date: "2016-11-30 06:25:06.000000",
timezone_type: 3,
timezone: "UTC"
}
}
>>> $article->toArray()
=> [
"title" => "标题1",
"body" => "文章内容1",
"published_at" => <Carbon\Carbon #0000000060c30169000000016bd2980b> {
date: "2016-11-30 06:25:06.000000",
timezone_type: 3,
timezone: "UTC"
}
]
>>> $article->toJson()
将刚才添加的数据存入数据库,用到的 save()
>>> $article->save()
=> true
>>> $article
=> <App\Article #0000000060c3016a000000016bd293a3> {
title: "标题1",
body: "文章内容1",
published_at: <Carbon\Carbon #0000000060c30169000000016bd2980b> {
date: "2016-11-30 06:25:06.000000",
timezone_type: 3,
timezone: "UTC"
},
updated_at: "2016-11-30 06:27:54",
created_at: "2016-11-30 06:27:54",
id: 1
}
save()
方法也可以用于更新表数据:
>>> $article->body = "更新内容1"
=> "更新内容1"
>>> $article->title = '更新标题1'
=> "更新标题1"
>>> $article->save()
=> true
>>> $article
=> <App\Article #0000000060c3016a000000016bd293a3> {
title: "更新标题1",
body: "更新内容1",
published_at: <Carbon\Carbon #0000000060c30169000000016bd2980b> {
date: "2016-11-30 06:25:06.000000",
timezone_type: 3,
timezone: "UTC"
},
updated_at: "2016-11-30 06:32:41",
created_at: "2016-11-30 06:27:54",
id: 1
}
保存后,我们就可以取出数据库的数据了,使用 all()
方法可以取出全部数据,同时也可以使用刚才的 toArray()
转化数据格式:
>>> App\Article::all()->toArray();
=> [
[
"id" => 1,
"title" => "标题1",
"body" => "文章内容1",
"published_at" => "2016-11-30 06:25:06",
"created_at" => "2016-11-30 06:27:54",
"updated_at" => "2016-11-30 06:27:54",
"excerpt" => null
]
]
也可以根据 ID
来寻找某条记录:find()
>>> $article = App\Article::find(1)
=> <App\Article #0000000060c3017f000000016bd293a3> {
id: 1,
title: "更新标题1",
body: "更新内容1",
published_at: "2016-11-30 06:25:06",
created_at: "2016-11-30 06:27:54",
updated_at: "2016-11-30 06:32:41",
excerpt: null
}
不根据 ID 的话,也可以自己定义条件来获取符合条件的所有记录:where()
>>> $article = App\Article::where('body','更新内容1')->get();
=> <Illuminate\Database\Eloquent\Collection #0000000060c30182000000016bd293a3> [
<App\Article #0000000060c30186000000016bd293a3> {
id: 1,
title: "更新标题1",
body: "更新内容1",
published_at: "2016-11-30 06:25:06",
created_at: "2016-11-30 06:27:54",
updated_at: "2016-11-30 06:32:41",
excerpt: null
}
]
也可以获取符合条件的第一条记录: first()
>>> $article = App\Article::where('body','更新内容1')->first();
刚才只是单条数据的操作,Laravel 也可以批量赋值创建:
>>> $article = App\Article::create(['title'=>'另一篇标题','body'=>'另一篇内容','published_at'=>Carbon\Carbon::now()]);
Illuminate\Database\Eloquent\MassAssignmentException with message 'title'
Laravel 默认是不允许这样做的,需要将允许批量创建的字段添加到 $fillable
属性中。
// Article.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $fillable = [
'title',
'body',
'published_at',
];
}
然后,重新启动 tinker 并执行刚才的命令:
$ php artisan tinker
Psy Shell v0.4.1 (PHP 5.6.22 — cli) by Justin Hileman
>>> $article = App\Article::create(['title'=>'另一篇标题','body'=>'另一篇内容','published_at'=>Carbon\Carbon::now()]);
=> <App\Article #0000000001dffeec0000000159ffa4c5> {
title: "另一篇标题",
body: "另一篇内容",
published_at: <Carbon\Carbon #0000000001dffeef0000000159ffaf6d> {
date: "2016-11-30 06:46:59.000000",
timezone_type: 3,
timezone: "UTC"
},
updated_at: "2016-11-30 06:46:59",
created_at: "2016-11-30 06:46:59",
id: 3
}
除了 save()
外,update()
方法也可以用于更新表的数据:
>>> $article = App\Article::find(1)
=> <App\Article #0000000001dffee20000000159ffa4c5> {
id: 1,
title: "更新标题1",
body: "更新内容1",
published_at: "2016-11-30 06:25:06",
created_at: "2016-11-30 06:27:54",
updated_at: "2016-11-30 06:32:41",
excerpt: null
}
>>> $article->update(['body'=>'更新2']);