Support the ongoing development of Laravel.io →
Database Eloquent IOC
Last updated 1 year ago.
0

The getProductCodeAttribute should work, though you won't be able to query it like you try. It will only exist on the model (e.g. $order->product_code). If you want this column to appear in the JSON, you should add this to your model:

protected $appends = array('product_code');

If you still need to query the product code (for instance if you want to sort on that column) you could do it like this:

Auth::user()->orders()->get(array('product_name', DB::raw("CONCAT(product_name, '-', product_type, '-XXX') as product_code")));
Last updated 1 year ago.
0
Solution

Thanks .. That would have been been really good to know 6 months ago. ;-)

Is there any documentation other than the laravel.com where all these magical protected variables are described and what you can do with them ?

I have tried reading over the API but you cant get much out of it unless you know how to even navigate it. I tried doing basic stuff like adding a variable to the model but there is just no way I would have some how known that I was supposed to do it like this.

protected $appends = array('product_code');

Im also going to forget that in 6 months time and have no way of looking it up ...

Last updated 1 year ago.
0

http://laravel.com/docs/eloquent - very bottom of the documentation on Eloquent Models.

It's not hard to do a google search (it's the first result for eloquent model appends) and it is in the official documentation page you should expect it to be on (ctrl-f is your friend)

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

abelgo abelgo Joined 15 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.