Skip to content

Commit 958f0ba

Browse files
committed
Fixing the order of headings because the old way is seriously dumb.
1 parent 8ef0a91 commit 958f0ba

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

eloquent.md

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ To register the error handler, listen for the `ModelNotFoundException`
8686
var_dump($user->name);
8787
}
8888

89-
Of course, you may also use the query builder aggregate functions.
90-
9189
#### Eloquent Aggregates
9290

91+
Of course, you may also use the query builder aggregate functions.
92+
9393
$count = User::where('votes', '>', 100)->count();
9494

9595
If you are unable to generate the query you need via the fluent interface, feel free to use `whereRaw`:
@@ -123,10 +123,10 @@ When creating a new model, you pass an array of attributes to the model construc
123123

124124
To get started, set the `fillable` or `guarded` properties on your model.
125125

126-
The `fillable` property specifies which attributes should be mass-assignable. This can be set at the class or instance level.
127-
128126
#### Defining Fillable Attributes On A Model
129127

128+
The `fillable` property specifies which attributes should be mass-assignable. This can be set at the class or instance level.
129+
130130
class User extends Eloquent {
131131

132132
protected $fillable = array('first_name', 'last_name', 'email');
@@ -135,20 +135,20 @@ The `fillable` property specifies which attributes should be mass-assignable. Th
135135

136136
In this example, only the three listed attributes will be mass-assignable.
137137

138-
The inverse of `fillable` is `guarded`, and serves as a "black-list" instead of a "white-list":
139-
140138
#### Defining Guarded Attributes On A Model
141139

140+
The inverse of `fillable` is `guarded`, and serves as a "black-list" instead of a "white-list":
141+
142142
class User extends Eloquent {
143143

144144
protected $guarded = array('id', 'password');
145145

146146
}
147147

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-
150148
#### Blocking All Attributes From Mass Assignment
151149

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+
152152
protected $guarded = array('*');
153153

154154
<a name="insert-update-delete"></a>
@@ -191,20 +191,20 @@ After saving or creating a new model that uses auto-incrementing IDs, you may re
191191
// Retrieve the user by the attributes, or instantiate a new instance...
192192
$user = User::firstOrNew(array('name' => 'John'));
193193

194-
To update a model, you may retrieve it, change an attribute, and use the `save` method:
195-
196194
#### Updating A Retrieved Model
197195

196+
To update a model, you may retrieve it, change an attribute, and use the `save` method:
197+
198198
$user = User::find(1);
199199

200200
$user->email = 'john@foo.com';
201201

202202
$user->save();
203203

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-
206204
#### Saving A Model And Relationships
207205

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+
208208
$user->push();
209209

210210
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
254254

255255
$table->softDeletes();
256256

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.
258258

259259
#### Forcing Soft Deleted Models Into Results
260260

261+
To force soft deleted models to appear in a result set, use the `withTrashed` method on the query:
262+
261263
$users = User::withTrashed()->where('account_id', 1)->get();
262264

263265
The `withTrashed` method may be used on a defined relationship:
@@ -310,10 +312,10 @@ By default, Eloquent will maintain the `created_at` and `updated_at` columns on
310312

311313
}
312314

313-
If you wish to customize the format of your timestamps, you may override the `getDateFormat` method in your model:
314-
315315
#### Providing A Custom Timestamp Format
316316

317+
If you wish to customize the format of your timestamps, you may override the `getDateFormat` method in your model:
318+
317319
class User extends Eloquent {
318320

319321
protected function getDateFormat()
@@ -326,10 +328,10 @@ If you wish to customize the format of your timestamps, you may override the `ge
326328
<a name="query-scopes"></a>
327329
## Query Scopes
328330

329-
Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with `scope`:
330-
331331
#### Defining A Query Scope
332332

333+
Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with `scope`:
334+
333335
class User extends Eloquent {
334336

335337
public function scopePopular($query)
@@ -380,10 +382,10 @@ Of course, your database tables are probably related to one another. For example
380382
<a name="one-to-one"></a>
381383
### One To One
382384

383-
A one-to-one relationship is a very basic relation. For example, a `User` model might have one `Phone`. We can define this relation in Eloquent:
384-
385385
#### Defining A One To One Relation
386386

387+
A one-to-one relationship is a very basic relation. For example, a `User` model might have one `Phone`. We can define this relation in Eloquent:
388+
387389
class User extends Eloquent {
388390

389391
public function phone()
@@ -472,10 +474,10 @@ Again, you may override the conventional foreign key by passing a second argumen
472474

473475
return $this->hasMany('Comment', 'foreign_key', 'local_key');
474476

475-
To define the inverse of the relationship on the `Comment` model, we use the `belongsTo` method:
476-
477477
#### Defining The Inverse Of A Relation
478478

479+
To define the inverse of the relationship on the `Comment` model, we use the `belongsTo` method:
480+
479481
class Comment extends Eloquent {
480482

481483
public function post()
@@ -597,10 +599,10 @@ Polymorphic relations allow a model to belong to more than one other model, on a
597599

598600
}
599601

600-
Now, we can retrieve the photos for either a staff member or an order:
601-
602602
#### Retrieving A Polymorphic Relation
603603

604+
Now, we can retrieve the photos for either a staff member or an order:
605+
604606
$staff = Staff::find(1);
605607

606608
foreach ($staff->photos as $photo)
@@ -618,10 +620,10 @@ However, the true "polymorphic" magic is when you access the staff or order from
618620

619621
The `imageable` relation on the `Photo` model will return either a `Staff` or `Order` instance, depending on which type of model owns the photo.
620622

621-
To help understand how this works, let's explore the database structure for a polymorphic relation:
622-
623623
#### Polymorphic Relation Table Structure
624624

625+
To help understand how this works, let's explore the database structure for a polymorphic relation:
626+
625627
staff
626628
id - integer
627629
name - string
@@ -641,10 +643,10 @@ The key fields to notice here are the `imageable_id` and `imageable_type` on the
641643
<a name="many-to-many-polymorphic-relations"></a>
642644
### Many To Many Polymorphic Relations
643645

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-
646646
#### Polymorphic Many To Many Relation Table Structure
647647

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+
648650
posts
649651
id - integer
650652
name - string
@@ -692,10 +694,10 @@ The `Tag` model may define a method for each of its relationships:
692694
<a name="querying-relations"></a>
693695
## Querying Relations
694696

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-
697697
#### Querying Relations When Selecting
698698

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+
699701
$posts = Post::has('comments')->get();
700702

701703
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
815817
<a name="inserting-related-models"></a>
816818
## Inserting Related Models
817819

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-
820820
#### Attaching A Related Model
821821

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+
822824
$comment = new Comment(array('message' => 'A new comment.'));
823825

824826
$post = Post::find(1);
@@ -855,16 +857,16 @@ Of course, the opposite of `attach` is `detach`:
855857

856858
$user->roles()->detach(1);
857859

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-
860860
#### Using Sync To Attach Many To Many Models
861861

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:
863863

864-
You may also associate other pivot table values with the given IDs:
864+
$user->roles()->sync(array(1, 2, 3));
865865

866866
#### Adding Pivot Data When Syncing
867867

868+
You may also associate other pivot table values with the given IDs:
869+
868870
$user->roles()->sync(array(1 => array('expires' => true)));
869871

870872
Sometimes you may wish to create a new related model and attach it in a single command. For this operation, you may use the `save` method:
@@ -925,10 +927,10 @@ If you want your pivot table to have automatically maintained `created_at` and `
925927

926928
return $this->belongsToMany('Role')->withTimestamps();
927929

928-
To delete all records on the pivot table for a model, you may use the `detach` method:
929-
930930
#### Deleting Records On A Pivot Table
931931

932+
To delete all records on the pivot table for a model, you may use the `detach` method:
933+
932934
User::find(1)->roles()->detach();
933935

934936
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
947949

948950
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.
949951

950-
For example, we may determine if a result set contains a given primary key using the `contains` method:
951-
952952
#### Checking If A Collection Contains A Key
953953

954+
For example, we may determine if a result set contains a given primary key using the `contains` method:
955+
954956
$roles = User::find(1)->roles;
955957

956958
if ($roles->contains(2))
@@ -968,10 +970,10 @@ If a collection is cast to a string, it will be returned as JSON:
968970

969971
$roles = (string) User::find(1)->roles;
970972

971-
Eloquent collections also contain a few helpful methods for looping and filtering the items they contain:
972-
973973
#### Iterating Collections
974974

975+
Eloquent collections also contain a few helpful methods for looping and filtering the items they contain:
976+
975977
$roles = $user->roles->each(function($role)
976978
{
977979
//
@@ -1008,10 +1010,10 @@ When filtering collections, the callback provided will be used as callback for [
10081010

10091011
$roles = $roles->sortBy('created_at');
10101012

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-
10131013
#### Returning A Custom Collection Type
10141014

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+
10151017
class User extends Eloquent {
10161018

10171019
public function newCollection(array $models = array())
@@ -1024,10 +1026,10 @@ Sometimes, you may wish to return a custom Collection object with your own added
10241026
<a name="accessors-and-mutators"></a>
10251027
## Accessors & Mutators
10261028

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-
10291029
#### Defining An Accessor
10301030

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+
10311033
class User extends Eloquent {
10321034

10331035
public function getFirstNameAttribute($value)
@@ -1039,10 +1041,10 @@ Eloquent provides a convenient way to transform your model attributes when getti
10391041

10401042
In the example above, the `first_name` column has an accessor. Note that the value of the attribute is passed to the accessor.
10411043

1042-
Mutators are declared in a similar fashion:
1043-
10441044
#### Defining A Mutator
10451045

1046+
Mutators are declared in a similar fashion:
1047+
10461048
class User extends Eloquent {
10471049

10481050
public function setFirstNameAttribute($value)
@@ -1080,19 +1082,19 @@ Eloquent models fire several events, allowing you to hook into various points in
10801082

10811083
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.
10821084

1083-
If `false` is returned from the `creating`, `updating`, `saving`, or `deleting` events, the action will be cancelled:
1084-
10851085
#### Cancelling Save Operations Via Events
10861086

1087+
If `false` is returned from the `creating`, `updating`, `saving`, or `deleting` events, the action will be cancelled:
1088+
10871089
User::creating(function($user)
10881090
{
10891091
if ( ! $user->isValid()) return false;
10901092
});
10911093

1092-
Eloquent models also contain a static `boot` method, which may provide a convenient place to register your event bindings.
1093-
10941094
#### Setting A Model Boot Method
10951095

1096+
Eloquent models also contain a static `boot` method, which may provide a convenient place to register your event bindings.
1097+
10961098
class User extends Eloquent {
10971099

10981100
public static function boot()
@@ -1132,10 +1134,10 @@ You may register an observer instance using the `observe` method:
11321134
<a name="converting-to-arrays-or-json"></a>
11331135
## Converting To Arrays / JSON
11341136

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-
11371137
#### Converting A Model To An Array
11381138

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+
11391141
$user = User::with('roles')->first();
11401142

11411143
return $user->toArray();
@@ -1144,25 +1146,25 @@ Note that entire collections of models may also be converted to arrays:
11441146

11451147
return User::all()->toArray();
11461148

1147-
To convert a model to JSON, you may use the `toJson` method:
1148-
11491149
#### Converting A Model To JSON
11501150

1151-
return User::find(1)->toJson();
1151+
To convert a model to JSON, you may use the `toJson` method:
11521152

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

11551155
#### Returning A Model From A Route
11561156

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+
11571159
Route::get('users', function()
11581160
{
11591161
return User::all();
11601162
});
11611163

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-
11641164
#### Hiding Attributes From Array Or JSON Conversion
11651165

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:
1167+
11661168
class User extends Eloquent {
11671169

11681170
protected $hidden = array('password');

0 commit comments

Comments
 (0)