Laravel Eloquent Model Attributes

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Unraveling Laravel Eloquent Model Attributes and Their Table Mappings Body: When working with Laravel's Eloquent models, knowing which attributes are associated with the database table is essential. In this blog post, we will explore how to identify such attributes and discuss their importance in saving data to the database. Let us consider an example scenario where you have a table named 'announcements' with columns like 'id', 'text', and "created_by". 1. Laravel Eloquent Models Your first step is to instantiate a model class that extends from Laravel's Eloquent Model class. In this case, we will create an instance of the Announcement model:
$announcement = new App\Models\Announcement();
2. Accessing and Setting Attributes After instantiating a new Announcement object, you can access its attributes by using dot notation or array syntax:
// Using dot notation
$announcement->text = "New announcement text";

// Using array syntax
$announcement['text'] = "New announcement text";
3. Determining if an Attribute Maps to the Table Now, let us examine how you can determine if an attribute maps to the table and can be saved to it. As mentioned earlier, the conventional way of checking for the existence of a particular object property (in this case, the 'created_by' attribute) is using 'isset()':
if(isset($announcement->created_by)) {
    // created_by exists and has been set
}
However, this approach is not sufficient for determining whether an attribute can be saved to the database since it only checks if a value has been explicitly set. 4. Using 'Tapable' Methods Laravel's Eloquent Model class provides methods that allow you to access and manipulate model attributes more efficiently, especially in light of their potential association with the table. You can use two tapable methods: 'tap' or 'when'. These methods help ensure that your queries run only if a specific condition is met, such as the existence of an attribute or the value being set to null for instance. Let us see how we could determine whether a particular attribute maps to the table using tap and when:
$announcement = new App\Models\Announcement();

// Using tap() method
$announcement->tap(function ($model) use ("created_by") {
    if (isset($model->created_by)) {
        // created_by exists and has been set
    } else {
        $model->created_by = 'admin';
    }
});

// Using when() method
$announcement = App\Models\Announcement::when($created_by, function ($model) use ("$created_by") {
    $model->created_by = "$created_by";
    return true;
})->create();
5. Conclusion Accessing and manipulating attributes in Laravel's Eloquent Model classes can be challenging, especially when determining whether an attribute maps to a table or not. In this blog post, we explored the various approaches available, focusing on using tapable methods like 'tap()' and 'when()'. By utilizing these tools, you can ensure that your code is efficient and follows best practices in Laravel development. For more insights into Laravel development and Eloquent models, visit https://laravelcompany.com and explore their informative blog posts.