You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -135,20 +135,20 @@ The `fillable` property specifies which attributes should be mass-assignable. Th
135
135
136
136
In this example, only the three listed attributes will be mass-assignable.
137
137
138
-
The inverse of `fillable` is `guarded`, and serves as a "black-list" instead of a "white-list":
139
-
140
138
#### Defining Guarded Attributes On A Model
141
139
140
+
The inverse of `fillable` is `guarded`, and serves as a "black-list" instead of a "white-list":
141
+
142
142
class User extends Eloquent {
143
143
144
144
protected $guarded = array('id', 'password');
145
145
146
146
}
147
147
148
-
In the example above, the `id` and `password` attributes may **not** be mass assigned. All other attributes will be mass assignable. You may also block **all** attributes from mass assignment using the guard property:
149
-
150
148
#### Blocking All Attributes From Mass Assignment
151
149
150
+
In the example above, the `id` and `password` attributes may **not** be mass assigned. All other attributes will be mass assignable. You may also block **all** attributes from mass assignment using the guard property:
151
+
152
152
protected $guarded = array('*');
153
153
154
154
<aname="insert-update-delete"></a>
@@ -191,20 +191,20 @@ After saving or creating a new model that uses auto-incrementing IDs, you may re
191
191
// Retrieve the user by the attributes, or instantiate a new instance...
To update a model, you may retrieve it, change an attribute, and use the `save` method:
195
-
196
194
#### Updating A Retrieved Model
197
195
196
+
To update a model, you may retrieve it, change an attribute, and use the `save` method:
197
+
198
198
$user = User::find(1);
199
199
200
200
$user->email = 'john@foo.com';
201
201
202
202
$user->save();
203
203
204
-
Sometimes you may wish to save not only a model, but also all of its relationships. To do so, you may use the `push` method:
205
-
206
204
#### Saving A Model And Relationships
207
205
206
+
Sometimes you may wish to save not only a model, but also all of its relationships. To do so, you may use the `push` method:
207
+
208
208
$user->push();
209
209
210
210
You may also run updates as queries against a set of models:
@@ -254,10 +254,12 @@ To add a `deleted_at` column to your table, you may use the `softDeletes` method
254
254
255
255
$table->softDeletes();
256
256
257
-
Now, when you call the `delete` method on the model, the `deleted_at` column will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results. To force soft deleted models to appear in a result set, use the `withTrashed` method on the query:
257
+
Now, when you call the `delete` method on the model, the `deleted_at` column will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results.
258
258
259
259
#### Forcing Soft Deleted Models Into Results
260
260
261
+
To force soft deleted models to appear in a result set, use the `withTrashed` method on the query:
To define the inverse of the relationship on the `Comment` model, we use the `belongsTo` method:
476
-
477
477
#### Defining The Inverse Of A Relation
478
478
479
+
To define the inverse of the relationship on the `Comment` model, we use the `belongsTo` method:
480
+
479
481
class Comment extends Eloquent {
480
482
481
483
public function post()
@@ -597,10 +599,10 @@ Polymorphic relations allow a model to belong to more than one other model, on a
597
599
598
600
}
599
601
600
-
Now, we can retrieve the photos for either a staff member or an order:
601
-
602
602
#### Retrieving A Polymorphic Relation
603
603
604
+
Now, we can retrieve the photos for either a staff member or an order:
605
+
604
606
$staff = Staff::find(1);
605
607
606
608
foreach ($staff->photos as $photo)
@@ -618,10 +620,10 @@ However, the true "polymorphic" magic is when you access the staff or order from
618
620
619
621
The `imageable` relation on the `Photo` model will return either a `Staff` or `Order` instance, depending on which type of model owns the photo.
620
622
621
-
To help understand how this works, let's explore the database structure for a polymorphic relation:
622
-
623
623
#### Polymorphic Relation Table Structure
624
624
625
+
To help understand how this works, let's explore the database structure for a polymorphic relation:
626
+
625
627
staff
626
628
id - integer
627
629
name - string
@@ -641,10 +643,10 @@ The key fields to notice here are the `imageable_id` and `imageable_type` on the
641
643
<aname="many-to-many-polymorphic-relations"></a>
642
644
### Many To Many Polymorphic Relations
643
645
644
-
In addition to traditional polymorphic relations, you may also specify many-to-many polymorphic relations. For example, a blog `Post` and `Video` model could share a polymorphic relation to a `Tag` model. First, let's examine the table structure:
645
-
646
646
#### Polymorphic Many To Many Relation Table Structure
647
647
648
+
In addition to traditional polymorphic relations, you may also specify many-to-many polymorphic relations. For example, a blog `Post` and `Video` model could share a polymorphic relation to a `Tag` model. First, let's examine the table structure:
649
+
648
650
posts
649
651
id - integer
650
652
name - string
@@ -692,10 +694,10 @@ The `Tag` model may define a method for each of its relationships:
692
694
<aname="querying-relations"></a>
693
695
## Querying Relations
694
696
695
-
When accessing the records for a model, you may wish to limit your results based on the existence of a relationship. For example, you wish to pull all blog posts that have at least one comment. To do so, you may use the `has` method:
696
-
697
697
#### Querying Relations When Selecting
698
698
699
+
When accessing the records for a model, you may wish to limit your results based on the existence of a relationship. For example, you wish to pull all blog posts that have at least one comment. To do so, you may use the `has` method:
700
+
699
701
$posts = Post::has('comments')->get();
700
702
701
703
You may also specify an operator and a count:
@@ -815,10 +817,10 @@ It is also possible to eagerly load related models directly from an already exis
815
817
<aname="inserting-related-models"></a>
816
818
## Inserting Related Models
817
819
818
-
You will often need to insert new related models. For example, you may wish to insert a new comment for a post. Instead of manually setting the `post_id` foreign key on the model, you may insert the new comment from its parent `Post` model directly:
819
-
820
820
#### Attaching A Related Model
821
821
822
+
You will often need to insert new related models. For example, you may wish to insert a new comment for a post. Instead of manually setting the `post_id` foreign key on the model, you may insert the new comment from its parent `Post` model directly:
823
+
822
824
$comment = new Comment(array('message' => 'A new comment.'));
823
825
824
826
$post = Post::find(1);
@@ -855,16 +857,16 @@ Of course, the opposite of `attach` is `detach`:
855
857
856
858
$user->roles()->detach(1);
857
859
858
-
You may also use the `sync` method to attach related models. The `sync` method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:
859
-
860
860
#### Using Sync To Attach Many To Many Models
861
861
862
-
$user->roles()->sync(array(1, 2, 3));
862
+
You may also use the `sync` method to attach related models. The `sync` method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:
863
863
864
-
You may also associate other pivot table values with the given IDs:
864
+
$user->roles()->sync(array(1, 2, 3));
865
865
866
866
#### Adding Pivot Data When Syncing
867
867
868
+
You may also associate other pivot table values with the given IDs:
To delete all records on the pivot table for a model, you may use the `detach` method:
929
-
930
930
#### Deleting Records On A Pivot Table
931
931
932
+
To delete all records on the pivot table for a model, you may use the `detach` method:
933
+
932
934
User::find(1)->roles()->detach();
933
935
934
936
Note that this operation does not delete records from the `roles` table, but only from the pivot table.
@@ -947,10 +949,10 @@ Laravel also allows you to define a custom Pivot model. To define a custom model
947
949
948
950
All multi-result sets returned by Eloquent, either via the `get` method or a `relationship`, will return a collection object. This object implements the `IteratorAggregate` PHP interface so it can be iterated over like an array. However, this object also has a variety of other helpful methods for working with result sets.
949
951
950
-
For example, we may determine if a result set contains a given primary key using the `contains` method:
951
-
952
952
#### Checking If A Collection Contains A Key
953
953
954
+
For example, we may determine if a result set contains a given primary key using the `contains` method:
955
+
954
956
$roles = User::find(1)->roles;
955
957
956
958
if ($roles->contains(2))
@@ -968,10 +970,10 @@ If a collection is cast to a string, it will be returned as JSON:
968
970
969
971
$roles = (string) User::find(1)->roles;
970
972
971
-
Eloquent collections also contain a few helpful methods for looping and filtering the items they contain:
972
-
973
973
#### Iterating Collections
974
974
975
+
Eloquent collections also contain a few helpful methods for looping and filtering the items they contain:
976
+
975
977
$roles = $user->roles->each(function($role)
976
978
{
977
979
//
@@ -1008,10 +1010,10 @@ When filtering collections, the callback provided will be used as callback for [
1008
1010
1009
1011
$roles = $roles->sortBy('created_at');
1010
1012
1011
-
Sometimes, you may wish to return a custom Collection object with your own added methods. You may specify this on your Eloquent model by overriding the `newCollection` method:
1012
-
1013
1013
#### Returning A Custom Collection Type
1014
1014
1015
+
Sometimes, you may wish to return a custom Collection object with your own added methods. You may specify this on your Eloquent model by overriding the `newCollection` method:
1016
+
1015
1017
class User extends Eloquent {
1016
1018
1017
1019
public function newCollection(array $models = array())
@@ -1024,10 +1026,10 @@ Sometimes, you may wish to return a custom Collection object with your own added
1024
1026
<aname="accessors-and-mutators"></a>
1025
1027
## Accessors & Mutators
1026
1028
1027
-
Eloquent provides a convenient way to transform your model attributes when getting or setting them. Simply define a `getFooAttribute` method on your model to declare an accessor. Keep in mind that the methods should follow camel-casing, even though your database columns are snake-case:
1028
-
1029
1029
#### Defining An Accessor
1030
1030
1031
+
Eloquent provides a convenient way to transform your model attributes when getting or setting them. Simply define a `getFooAttribute` method on your model to declare an accessor. Keep in mind that the methods should follow camel-casing, even though your database columns are snake-case:
1032
+
1031
1033
class User extends Eloquent {
1032
1034
1033
1035
public function getFirstNameAttribute($value)
@@ -1039,10 +1041,10 @@ Eloquent provides a convenient way to transform your model attributes when getti
1039
1041
1040
1042
In the example above, the `first_name` column has an accessor. Note that the value of the attribute is passed to the accessor.
1041
1043
1042
-
Mutators are declared in a similar fashion:
1043
-
1044
1044
#### Defining A Mutator
1045
1045
1046
+
Mutators are declared in a similar fashion:
1047
+
1046
1048
class User extends Eloquent {
1047
1049
1048
1050
public function setFirstNameAttribute($value)
@@ -1080,19 +1082,19 @@ Eloquent models fire several events, allowing you to hook into various points in
1080
1082
1081
1083
Whenever a new item is saved for the first time, the `creating` and `created` events will fire. If an item is not new and the `save` method is called, the `updating` / `updated` events will fire. In both cases, the `saving` / `saved` events will fire.
1082
1084
1083
-
If `false` is returned from the `creating`, `updating`, `saving`, or `deleting` events, the action will be cancelled:
1084
-
1085
1085
#### Cancelling Save Operations Via Events
1086
1086
1087
+
If `false` is returned from the `creating`, `updating`, `saving`, or `deleting` events, the action will be cancelled:
1088
+
1087
1089
User::creating(function($user)
1088
1090
{
1089
1091
if ( ! $user->isValid()) return false;
1090
1092
});
1091
1093
1092
-
Eloquent models also contain a static `boot` method, which may provide a convenient place to register your event bindings.
1093
-
1094
1094
#### Setting A Model Boot Method
1095
1095
1096
+
Eloquent models also contain a static `boot` method, which may provide a convenient place to register your event bindings.
1097
+
1096
1098
class User extends Eloquent {
1097
1099
1098
1100
public static function boot()
@@ -1132,10 +1134,10 @@ You may register an observer instance using the `observe` method:
1132
1134
<aname="converting-to-arrays-or-json"></a>
1133
1135
## Converting To Arrays / JSON
1134
1136
1135
-
When building JSON APIs, you may often need to convert your models and relationships to arrays or JSON. So, Eloquent includes methods for doing so. To convert a model and its loaded relationship to an array, you may use the `toArray` method:
1136
-
1137
1137
#### Converting A Model To An Array
1138
1138
1139
+
When building JSON APIs, you may often need to convert your models and relationships to arrays or JSON. So, Eloquent includes methods for doing so. To convert a model and its loaded relationship to an array, you may use the `toArray` method:
1140
+
1139
1141
$user = User::with('roles')->first();
1140
1142
1141
1143
return $user->toArray();
@@ -1144,25 +1146,25 @@ Note that entire collections of models may also be converted to arrays:
1144
1146
1145
1147
return User::all()->toArray();
1146
1148
1147
-
To convert a model to JSON, you may use the `toJson` method:
1148
-
1149
1149
#### Converting A Model To JSON
1150
1150
1151
-
return User::find(1)->toJson();
1151
+
To convert a model to JSON, you may use the `toJson` method:
1152
1152
1153
-
Note that when a model or collection is cast to a string, it will be converted to JSON, meaning you can return Eloquent objects directly from your application's routes!
1153
+
return User::find(1)->toJson();
1154
1154
1155
1155
#### Returning A Model From A Route
1156
1156
1157
+
Note that when a model or collection is cast to a string, it will be converted to JSON, meaning you can return Eloquent objects directly from your application's routes!
1158
+
1157
1159
Route::get('users', function()
1158
1160
{
1159
1161
return User::all();
1160
1162
});
1161
1163
1162
-
Sometimes you may wish to limit the attributes that are included in your model's array or JSON form, such as passwords. To do so, add a `hidden` property definition to your model:
1163
-
1164
1164
#### Hiding Attributes From Array Or JSON Conversion
1165
1165
1166
+
Sometimes you may wish to limit the attributes that are included in your model's array or JSON form, such as passwords. To do so, add a `hidden` property definition to your model:
0 commit comments