Can you iterate over the properties of an eloquent object?

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 those properties... something like this:

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

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

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


You can also use getAttributes() method:

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

Need Your Help

Java only allow two drawn circles to be selected at a time, how to unselect

java swing user-interface select unselect

Hey so I've got 20 circles displayed on a screen, I want the user to be able to select only two circles and be able to unselect circles but still only have maximum 2 circles selected. I know how to...

Set description on DataContract members

wcf c#-4.0 serialization

I would like to control whats on the help pages, and especially information about each field that. For example, I have this class:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.