How to hide relationship columns in laravel?

by arnoldo.moen , in category: PHP Frameworks , 6 months ago

How to hide relationship columns in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 6 months ago

@arnoldo.moen 

To hide relationship columns in Laravel, you can use the $hidden property in your model class. The $hidden property is an array that specifies which attributes should be hidden when the model is converted to an array or JSON.


For example, let's say you have a User model with a relationship to the Role model. You can hide the role_id column by adding it to the $hidden property in the User model:

1
2
3
4
class User extends Model
{
    protected $hidden = ['role_id'];
}


Now, when you retrieve a User model and convert it to an array or JSON, the role_id column will not be included in the output.


Additionally, you can also use the $visible property to specify which attributes should be visible, and the $fillable property to specify which attributes are mass assignable.