I'm currently working on a project where I'm writing an API, and with a lot of the models, I'm trying to hide their parent relationships from being returned, like so
<?php namespace Viper\Model;
class User_Token extends Eloquent {
protected $table = 'users_tokens';
protected $fillable = array(
'user_id', 'token'
);
protected $hidden = array(
'id', 'user_id', 'user'
);
public function user() {
return $this->belongsTo('User');
}
}
In the Laravel documentation, for the Eloquent > Converting to Array or Json section, it clearly says
Note: When hiding relationships, use the relationship's method name, not the dynamic accessor name.
What exactly does this mean? In the above example, both the method name and the dynamic accessor name are the same, and I can't for the life of me, think of a situation where this wouldn't be the case.
protected $hidden = array(
'id', 'user_id', 'user'
^^^ relationship's method name which is "user"
);
if you want to hide the relationship , you have to include method name
under hidden attributes. From your hidden attributes, i can see, you are perfectly hiding user
relationship from Array and JSON conversion . However, if you have 'user' column in your users_tokens
table, I have no idea what Laravel will behave.
public function user() {
return $this->belongsTo('User');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With