last day (15 days later) » 

6:52 PM
0
A: Laravel 4 Present Model

damianiUse an accessor in your Model. To concatenate A and B, for instance: public function getPropertyCAttribute() { return $this->attributes['property_a'] . ' ' . $this->attributes['property_b']; } And then you can access Model::find($id)->propertyC. If the function is something that can be ...

 
Should propertyC appear when I call Model::all()?
 
Yes, once you loop over the results to access individual rows. So if you called $model=Model::all(), then you have access to propertyC inside foreach ($model ad $m) { var_dump($m->propertyC) };. Also, make sure the value in $this->attributes[''] is the actual field name in your database; it is case-sensitive, so if your column is called property_a, the accessor should reference $this->attributes['property_a'];
 
Say I have this function: public function getFooAttribute() { return "goooglelelgoo"; } Then should I always have a property called Foo returned?
 
Yep, for each $element of your result set, at $element->foo.
 
Just not happening for me at the moment, I have updated my question with my full model.
 
6:52 PM
Your model is correct. Show the code where you call DiscountLink::all() and try to access foo.
 
HI damiani, thanks so much for helping me with this.
 
Sure.
So if you loop over your results, you should be able to access foo on any individual row.
 
So I can't simply return the results with all()
because this is a RESTful interface so I just want to spit out some JSON to my JavaScript
 
all() gets every record. It's a collection of records, but to return any result, you need to access an individual record.
 
So I need to loop over and recreate the JSON myself?
Is there anything more precise to do this?
 
6:58 PM
Oh, I didn't know you were trying to send the whole model as JSON. How are you converting the model to JSON at the moment?
 
It's taken care for me automatically using the model which extends Eloquent
This is part of a RESTful interface that I'm linking to AngularJS
 
I see. The property foo is accessible if you call it on an individual record in your result set, but I'm not sure how to automatically add it to your json. What is the nature of the function? Would it be possible to use SQL functions to return your new value?
 
I could do for now but things could get more tricky in the future. And i wanted to leverage some Laravel functions.
 
I think I have an answer...let me test it.
 
ok great
thank you sir!
 
7:06 PM
Yep. Buried at the end of the docs page on Eloquent. Add this line to your model, and the attribute will be added to the results collection: protected $appends = array('foo');
 
Ah perfect! Thanks so much for your help. If you update your answer, I'll give you a voteup etc... There should be extra credit for this kinda help!
 
Learn something new every day. Answer updated...Good luck!
 
7:29 PM
Definitely! All the best!
Heres another one for you incase you're interested:
Also you can write class, which extends eloquent model, and add this feature to this class. And extends your all model from this new class. But ask on laravel.io is better) — podliy16 3 hours ago
0
Q: Input Sanitising Laravel 4

Savvas NicholasI am using Laravel 4 to create a RESTful interface for my AngularJS app. At the moment, I want to update an object. My model is called Discount Link The way I do this is: $data = Input::all(); $affectedRows = DiscountLink::where('id', '=', $id)->update($data); (Please imagine there ar...

 

last day (15 days later) »