Laravel get model attribute: difference between two methods

Laravel get model attribute: difference between two methods

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Model Attributes in Laravel: Accessors vs. Direct Property Retrieval

In Laravel, retrieving a model attribute can be done through direct property access or by defining a custom accessor method. While both approaches yield a value on the surface, they serve distinctly different purposes within the framework's architecture. Understanding when to use $model->username versus $model->getUsername() is critical for maintaining clean separation of concerns and adhering to Laravel's naming conventions.

Direct Property Access

When you access a model attribute directly using $user->username, you are simply reading the value of that specific column from the underlying database record. This is the most straightforward approach when you need raw data without transformation.

// In a Blade template or controller
{{ Auth::user()->username }}

This syntax maps directly to the Eloquent property $this->attributes['username']. It is efficient and requires no additional configuration. However, it exposes the column name exactly as stored in the database. If your database contains a column named full_name but you want to display only the first name, direct access cannot filter or transform that data on the fly without creating a new accessor.

Custom Accessors with Magic Methods

The second method involves defining a public function within the model class that follows a specific naming convention. For an attribute named username, the corresponding method must be named getUsername().

// In User.php
public function getUsername() {
    return $this->username;
}

Laravel utilizes magic methods to intercept property access. When you call $user->getUsername(), Eloquent checks for a public method matching the pattern get{AttributeName}(). If found, it executes that method and returns its result. This allows you to inject logic into how an attribute is displayed.

For example, if username needs to be capitalized or combined with a suffix before display:

public function getUsername() {
    return ucfirst($this->username) . ' (Guest)';
}

Now, when the Blade template calls {{ Auth::user()->getUsername() }}, it renders the transformed string. This is the standard way to handle attribute mutation or formatting in Eloquent models.

The Critical Distinction: Magic Methods vs. Explicit Calls

The fundamental difference lies in the invocation mechanism and the expectation of return values. Direct property access ($model->username) relies on Laravel's internal magic methods that automatically trigger getAttribute() under the hood. It is strictly for reading.

Conversely, calling $model->getUsername() explicitly invokes a public method you define. While you can technically make a custom accessor return the raw attribute value (as shown in the first example), doing so adds unnecessary verbosity to your views and controllers. The explicit method call suggests intent: "I want logic applied here." If there is no logic, the direct property access is preferred for simplicity.

Naming Conventions and Pitfalls

A common pitfall arises from confusing these two approaches when trying to implement mutators. Mutators are used to set attribute values with transformed data upon assignment (e.g., user->username = 'john' becomes 'John Doe' in the database). The naming convention for mutators follows a different pattern: setUsernameAttribute().

If you define a method named setUsername() instead of setUsernameAttribute(), Laravel will not treat it as a mutator. Furthermore, you cannot use direct property assignment to trigger a custom setter unless you are specifically calling the mutator method directly, which is generally discouraged in favor of Eloquent's magic behavior.

When to Use Each Approach

Use direct property access (->username) whenever you need the raw data or when the attribute represents a column name that requires no transformation. This keeps your views and controllers concise.

Use custom accessor methods (->getUsername()) when you need to format, filter, or compute values based on other columns before displaying them. This keeps business logic centralized within the model rather than scattering it across views.

By adhering to these patterns, you ensure that your code remains readable and that the separation between data storage (the database) and data presentation (the view) is maintained effectively.

Our network