Dhirender
Dhirender

Reputation: 634

Accessor not working in Laravel 5.4

I am new on Laravel 5.4 and currently working on small project on Laravel. As per my client requirements, I have renamed some of columns of tables with CamelCase standard which is against of Laravel naming convention.

For example, My table "A" has columns "Status" and "FirstName" instead of "status" and "first_name".

Now, I am using accessor in model file to modify value of these columns by creating function given below:

public function getStatusAttribute($val) {  
   reutrn $val . ' Status';  
}  

public function getFirstNameAttribute($val) {  
   return 'User is '.$val;  
}  

When I am fetching data using elequent process, then system is returning value of these as blank.

If I rename the column name according to "Laravel Naming Convention" then it is giving me right results.

I don't know what is the exact issue here. Please anyone can share your solution to fix this problem?

Upvotes: 0

Views: 553

Answers (1)

Sandeesh
Sandeesh

Reputation: 11906

Access the fields directly.

public function getStatusAttribute()
{
    return $this->Status . ' Status';
}

public function getFirstNameAttribute()
{
    return 'User is ' . $this->FirstName;
}

Add this to the model to have these fields appended every time.

protected $appends = [
    'status', 'first_name',
];

Upvotes: 1

Related Questions