hedeqiang 4年前

修改理由:

排版缩进对齐

相关信息:


此投稿已在 4年前 合并。

内容修改:

红色背景 为原始内容

绿色背景 为新增或者修改的内容

OldNewDifferences
22
33```
44// 定义一个 Eloquent 模型
5  class User extends Model {}
 5class User extends Model {}
66// 生成一个 Eloquent 模型
77php artisan make:model User
88// 指定一个自定义的数据表名称
9  class User extends Model {
 9class User extends Model {
1010 protected $table = 'my_users';
1111}
1212```
 
1616```
1717Model::create(array('key' => 'value'));
1818// 通过属性找到第一条相匹配的数据或创造一条新数据
19  Model::firstOrCreate(array('key' => 'value'));
 19Model::firstOrCreate(array('key' => 'value'));
2020// 通过属性找到第一条相匹配的数据或实例化一条新数据
21  Model::firstOrNew(array('key' => 'value'));
 21Model::firstOrNew(array('key' => 'value'));
2222// 通过属性找到相匹配的数据并更新,如果不存在即创建
23  Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value'));
 23Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value'));
2424// 使用属性的数组来填充一个模型, 用的时候要小心「Mass Assignment」安全问题 !
25  Model::fill($attributes);
 25Model::fill($attributes);
2626Model::destroy(1);
2727Model::all();
2828Model::find(1);
2929// 使用双主键进行查找
30  Model::find(array('first', 'last'));
 30Model::find(array('first', 'last'));
3131// 查找失败时抛出异常
32  Model::findOrFail(1);
 32Model::findOrFail(1);
3333// 使用双主键进行查找, 失败时抛出异常
34  Model::findOrFail(array('first', 'last'));
 34Model::findOrFail(array('first', 'last'));
3535Model::where('foo', '=', 'bar')->get();
3636Model::where('foo', '=', 'bar')->first();
3737Model::where('foo', '=', 'bar')->exists();
3838// 动态属性查找
39  Model::whereFoo('bar')->first();
 39Model::whereFoo('bar')->first();
4040// 查找失败时抛出异常
41  Model::where('foo', '=', 'bar')->firstOrFail();
 41Model::where('foo', '=', 'bar')->firstOrFail();
4242Model::where('foo', '=', 'bar')->count();
4343Model::where('foo', '=', 'bar')->delete();
4444// 输出原始的查询语句
45  Model::where('foo', '=', 'bar')->toSql();
 45Model::where('foo', '=', 'bar')->toSql();
4646Model::whereRaw('foo = bar and cars = 2', array(20))->get();
4747Model::on('connection-name')->find(1);
4848Model::with('relation')->get();
4949Model::all()->take(10);
5050Model::all()->skip(10);
5151// 默认的 Eloquent 排序是上升排序
52  Model::all()->orderBy('column');
 52Model::all()->orderBy('column');
5353Model::all()->orderBy('column','desc');
5454```
5555
 
5858```
5959Model::withTrashed()->where('cars', 2)->get();
6060// 在查询结果中包括带被软删除的模型
61  Model::withTrashed()->where('cars', 2)->restore();
 61Model::withTrashed()->where('cars', 2)->restore();
6262Model::where('cars', 2)->forceDelete();
6363// 查找只带有软删除的模型
64  Model::onlyTrashed()->where('cars', 2)->get();
 64Model::onlyTrashed()->where('cars', 2)->get();
6565```
6666
6767### 模型关联
6868
6969```
7070// 一对一 - User::phone()
71  return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
 71return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
7272// 一对一 - Phone::user(), 定义相对的关联
73  return $this->belongsTo('App\User', 'foreign_key', 'other_key');
 73return $this->belongsTo('App\User', 'foreign_key', 'other_key');
7474
7575// 一对多 - Post::comments()
76  return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
 76return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
7777// 一对多 - Comment::post()
78  return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
 78return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
7979
8080// 多对多 - User::roles();
81  return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
 81return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
8282// 多对多 - Role::users();
83  return $this->belongsToMany('App\User');
 83return $this->belongsToMany('App\User');
8484// 多对多 - Retrieving Intermediate Table Columns
8585$role->pivot->created_at;
8686// 多对多 - 中介表字段
87  return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
 87return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
8888// 多对多 - 自动维护 created_at 和 updated_at 时间戳
89  return $this->belongsToMany('App\Role')->withTimestamps();
 89return $this->belongsToMany('App\Role')->withTimestamps();
9090
9191// 远层一对多 - Country::posts(), 一个 Country 模型可能通过中介的 Users
92  // 模型关联到多个 Posts 模型(User::country_id)
93  return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
 92// 模型关联到多个 Posts 模型(User::country_id)
 93return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
9494
9595// 多态关联 - Photo::imageable()
96  return $this->morphTo();
 96return $this->morphTo();
9797// 多态关联 - Staff::photos()
98  return $this->morphMany('App\Photo', 'imageable');
 98return $this->morphMany('App\Photo', 'imageable');
9999// 多态关联 - Product::photos()
100  return $this->morphMany('App\Photo', 'imageable');
 100return $this->morphMany('App\Photo', 'imageable');
101101// 多态关联 - 在 AppServiceProvider 中注册你的「多态对照表」
102  Relation::morphMap([
 102Relation::morphMap([
103103   'Post' => App\Post::class,
104104   'Comment' => App\Comment::class,
105105]);
106106
107107// 多态多对多关联 - 涉及数据库表: posts,videos,tags,taggables
108  // Post::tags()
109  return $this->morphToMany('App\Tag', 'taggable');
 108// Post::tags()
 109return $this->morphToMany('App\Tag', 'taggable');
110110// Video::tags()
111  return $this->morphToMany('App\Tag', 'taggable');
 111return $this->morphToMany('App\Tag', 'taggable');
112112// Tag::posts()
113  return $this->morphedByMany('App\Post', 'taggable');
 113return $this->morphedByMany('App\Post', 'taggable');
114114// Tag::videos()
115  return $this->morphedByMany('App\Video', 'taggable');
 115return $this->morphedByMany('App\Video', 'taggable');
116116
117117// 查找关联
118118$user->posts()->where('active', 1)->get();
 
189189
190190```
191191// 关闭模型插入或更新操作引发的 「mass assignment」异常
192  Eloquent::unguard();
 192Eloquent::unguard();
193193// 重新开启「mass assignment」异常抛出功能
194  Eloquent::reguard();
 194Eloquent::reguard();
195195```