Can you go through the properties of the object of eloquence?

If I have, for example:

$project = new Project(); // Project is a class that extends Eloquent $project->title; $project->something; 

Is it possible to iterate over these properties ... something like this:

 foreach( $project as $key => $value ) { echo $key; } 

I am trying to do this in order to achieve a unit of work for editing Eloquent models

+5
source share
2 answers

You can use the toArray() method:

 foreach( $project->toArray() as $key => $value ) { echo $key; } 

http://laravel.com/docs/4.2/eloquent#converting-to-arrays-or-json

+8
source

You can also use the getAttributes() method:

 foreach ($project->getAttributes() as $k => $v) { echo $k.' '.$v."<br />"; } 
+3
source

Source: https://habr.com/ru/post/1203800/


All Articles