Search code examples
phplaravellaravel-4

Laravel Eloquent Model Attributes


After instantiating a model class that extends Laravel's Eloquent Model class, is there a way to determine if the property/attribute maps to the table, and therefore can be saved to the table?

For example, I have a table announcements with columns id, text and "created_by".

How can I know created_by is an attribute and will be saved if set?

$announcement = new Announcement();

isset($announcement->created_by) understandably returns false if I haven't explicitly set the value yet. I have tried various functions inherited from the Eloquent model class, but so far none have worked. I'm looking for something like:

$announcement->doesThisMapToMyTable('created_by') that returns true whether or not $announcement->created_by has been set.


Solution

  • For Laravel 5.4:

    $hasCreatedAt = \Schema::hasColumn(app(Model::class)->getTable(), 'created_at');
    
        if($hasCreatedAt == true) {
           ...
        }