Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
17cabd47
Commit
17cabd47
authored
Mar 29, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added to_array() and $hidden variable to the Eloquent base model.
Signed-off-by:
Taylor Otwell
<
taylorotwell@gmail.com
>
parent
023dacf5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
model.php
laravel/database/eloquent/model.php
+52
-0
No files found.
laravel/database/eloquent/model.php
View file @
17cabd47
...
...
@@ -55,6 +55,13 @@ abstract class Model {
*/
public
static
$accessible
;
/**
* The attributes that should be excluded from to_array.
*
* @var array
*/
public
static
$hidden
=
array
();
/**
* Indicates if the model has update and creation timestamps.
*
...
...
@@ -520,6 +527,51 @@ abstract class Model {
unset
(
$this
->
attributes
[
$key
]);
}
/**
* Get the model attributes and relationships in array form.
*
* @return array
*/
public
function
to_array
()
{
$attributes
=
array
();
// First we need to gather all of the regular attributes. If the attribute
// exists in the array of "hidden" attributes, it will not be added to
// the array so we can easily exclude things like passwords, etc.
foreach
(
array_keys
(
$this
->
attributes
)
as
$attribute
)
{
if
(
!
in_array
(
$attribute
,
static
::
$hidden
))
{
$attributes
[
$attribute
]
=
$this
->
$attribute
;
}
}
foreach
(
$this
->
relationships
as
$name
=>
$models
)
{
// If the relationship is not a "to-many" relationship, we can just
// to_array the related model and add it as an attribute to the
// array of existing regular attributes we gathered.
if
(
!
is_array
(
$models
))
{
$attributes
[
$name
]
=
$models
->
to_array
();
}
// If the relationship is a "to-many" relationship we need to spin
// through each of the related models and add each one with the
// to_array method, keying them both by name and ID.
else
{
foreach
(
$models
as
$id
=>
$model
)
{
$attributes
[
$name
][
$id
]
=
$model
->
to_array
();
}
}
}
return
$attributes
;
}
/**
* Handle the dynamic retrieval of attributes and associations.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment