排版缩进对齐
hedeqiang
4年前
修改理由:
相关信息:
- 类型:文档文章
- 文章: Model
- 文档: 《Laravel 速查表(5.8)》
此投稿已在 4年前 合并。
内容修改:
Old | New | Differences |
---|---|---|
2 | 2 | |
3 | 3 | ``` |
4 | 4 | // 定义一个 Eloquent 模型 |
5 | ||
5 | class User extends Model {} | |
6 | 6 | // 生成一个 Eloquent 模型 |
7 | 7 | php artisan make:model User |
8 | 8 | // 指定一个自定义的数据表名称 |
9 | ||
9 | class User extends Model { | |
10 | 10 | protected $table = 'my_users'; |
11 | 11 | } |
12 | 12 | ``` | … | … |
16 | 16 | ``` |
17 | 17 | Model::create(array('key' => 'value')); |
18 | 18 | // 通过属性找到第一条相匹配的数据或创造一条新数据 |
19 | ||
19 | Model::firstOrCreate(array('key' => 'value')); | |
20 | 20 | // 通过属性找到第一条相匹配的数据或实例化一条新数据 |
21 | ||
21 | Model::firstOrNew(array('key' => 'value')); | |
22 | 22 | // 通过属性找到相匹配的数据并更新,如果不存在即创建 |
23 | ||
23 | Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value')); | |
24 | 24 | // 使用属性的数组来填充一个模型, 用的时候要小心「Mass Assignment」安全问题 ! |
25 | ||
25 | Model::fill($attributes); | |
26 | 26 | Model::destroy(1); |
27 | 27 | Model::all(); |
28 | 28 | Model::find(1); |
29 | 29 | // 使用双主键进行查找 |
30 | ||
30 | Model::find(array('first', 'last')); | |
31 | 31 | // 查找失败时抛出异常 |
32 | ||
32 | Model::findOrFail(1); | |
33 | 33 | // 使用双主键进行查找, 失败时抛出异常 |
34 | ||
34 | Model::findOrFail(array('first', 'last')); | |
35 | 35 | Model::where('foo', '=', 'bar')->get(); |
36 | 36 | Model::where('foo', '=', 'bar')->first(); |
37 | 37 | Model::where('foo', '=', 'bar')->exists(); |
38 | 38 | // 动态属性查找 |
39 | ||
39 | Model::whereFoo('bar')->first(); | |
40 | 40 | // 查找失败时抛出异常 |
41 | ||
41 | Model::where('foo', '=', 'bar')->firstOrFail(); | |
42 | 42 | Model::where('foo', '=', 'bar')->count(); |
43 | 43 | Model::where('foo', '=', 'bar')->delete(); |
44 | 44 | // 输出原始的查询语句 |
45 | ||
45 | Model::where('foo', '=', 'bar')->toSql(); | |
46 | 46 | Model::whereRaw('foo = bar and cars = 2', array(20))->get(); |
47 | 47 | Model::on('connection-name')->find(1); |
48 | 48 | Model::with('relation')->get(); |
49 | 49 | Model::all()->take(10); |
50 | 50 | Model::all()->skip(10); |
51 | 51 | // 默认的 Eloquent 排序是上升排序 |
52 | ||
52 | Model::all()->orderBy('column'); | |
53 | 53 | Model::all()->orderBy('column','desc'); |
54 | 54 | ``` |
55 | 55 | … | … |
58 | 58 | ``` |
59 | 59 | Model::withTrashed()->where('cars', 2)->get(); |
60 | 60 | // 在查询结果中包括带被软删除的模型 |
61 | ||
61 | Model::withTrashed()->where('cars', 2)->restore(); | |
62 | 62 | Model::where('cars', 2)->forceDelete(); |
63 | 63 | // 查找只带有软删除的模型 |
64 | ||
64 | Model::onlyTrashed()->where('cars', 2)->get(); | |
65 | 65 | ``` |
66 | 66 | |
67 | 67 | ### 模型关联 |
68 | 68 | |
69 | 69 | ``` |
70 | 70 | // 一对一 - User::phone() |
71 | ||
71 | return $this->hasOne('App\Phone', 'foreign_key', 'local_key'); | |
72 | 72 | // 一对一 - Phone::user(), 定义相对的关联 |
73 | ||
73 | return $this->belongsTo('App\User', 'foreign_key', 'other_key'); | |
74 | 74 | |
75 | 75 | // 一对多 - Post::comments() |
76 | ||
76 | return $this->hasMany('App\Comment', 'foreign_key', 'local_key'); | |
77 | 77 | // 一对多 - Comment::post() |
78 | ||
78 | return $this->belongsTo('App\Post', 'foreign_key', 'other_key'); | |
79 | 79 | |
80 | 80 | // 多对多 - User::roles(); |
81 | ||
81 | return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id'); | |
82 | 82 | // 多对多 - Role::users(); |
83 | ||
83 | return $this->belongsToMany('App\User'); | |
84 | 84 | // 多对多 - Retrieving Intermediate Table Columns |
85 | 85 | $role->pivot->created_at; |
86 | 86 | // 多对多 - 中介表字段 |
87 | ||
87 | return $this->belongsToMany('App\Role')->withPivot('column1', 'column2'); | |
88 | 88 | // 多对多 - 自动维护 created_at 和 updated_at 时间戳 |
89 | ||
89 | return $this->belongsToMany('App\Role')->withTimestamps(); | |
90 | 90 | |
91 | 91 | // 远层一对多 - Country::posts(), 一个 Country 模型可能通过中介的 Users |
92 | ||
93 | ||
92 | // 模型关联到多个 Posts 模型(User::country_id) | |
93 | return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id'); | |
94 | 94 | |
95 | 95 | // 多态关联 - Photo::imageable() |
96 | ||
96 | return $this->morphTo(); | |
97 | 97 | // 多态关联 - Staff::photos() |
98 | ||
98 | return $this->morphMany('App\Photo', 'imageable'); | |
99 | 99 | // 多态关联 - Product::photos() |
100 | ||
100 | return $this->morphMany('App\Photo', 'imageable'); | |
101 | 101 | // 多态关联 - 在 AppServiceProvider 中注册你的「多态对照表」 |
102 | ||
102 | Relation::morphMap([ | |
103 | 103 | 'Post' => App\Post::class, |
104 | 104 | 'Comment' => App\Comment::class, |
105 | 105 | ]); |
106 | 106 | |
107 | 107 | // 多态多对多关联 - 涉及数据库表: posts,videos,tags,taggables |
108 | ||
109 | ||
108 | // Post::tags() | |
109 | return $this->morphToMany('App\Tag', 'taggable'); | |
110 | 110 | // Video::tags() |
111 | ||
111 | return $this->morphToMany('App\Tag', 'taggable'); | |
112 | 112 | // Tag::posts() |
113 | ||
113 | return $this->morphedByMany('App\Post', 'taggable'); | |
114 | 114 | // Tag::videos() |
115 | ||
115 | return $this->morphedByMany('App\Video', 'taggable'); | |
116 | 116 | |
117 | 117 | // 查找关联 |
118 | 118 | $user->posts()->where('active', 1)->get(); | … | … |
189 | 189 | |
190 | 190 | ``` |
191 | 191 | // 关闭模型插入或更新操作引发的 「mass assignment」异常 |
192 | ||
192 | Eloquent::unguard(); | |
193 | 193 | // 重新开启「mass assignment」异常抛出功能 |
194 | ||
194 | Eloquent::reguard(); | |
195 | 195 | ``` |