I have two models: Uai
and Information
with an hasOne relationship.
Uai (uai_id, a)
Information (uai_id, b)
I want to generate a Json response witch contains :
Uai
records true
if the hasOne Relationship exists between Uai
and Information
and false
if there is no relationshipAny idea ?
Thanks in advance Paguemaou
Edit One
Thanks jedrzej.kurylo for your answer.
How can I use the uai_id
of the current row in the fake column getter
? I othen use getters and seeters but I never try to use the content of another column. Can you give me an example ?
If I understand, I can use the fake column name
in a select like others columns name. I am true ?
Eloquent allows you to easily add custom field to a model's JSON representation.
Firstly, you need to define a list of additional fields by defining $appends property in your model class:
protected $appends = ['fakeColumnName'];
Secondly, add a getter for the fake column that will provide the value for custom column:
public function getFakeColumnNameAttribute() {
//here add the code that will return custom column's value
}
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