##Problem I have an eloquent model "Invoice" that hasMany "InvoiceTicket" that belongsTo "Ticket". The Ticket row contains the cost of a ticket. I am trying to write a method in Invoice to get the sum cost of all tickets in an invoice. How do I go down two nested levels to get this?
Thanks in advance for any help, Code examples are much appreciated. Jeff
##Errors
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cost' in 'field list' (SQL: select SUM(cost) AS costs from invoice_ticket
)
##Behavior Expected Expecting the sum of all cost fields.
##Actual It is looking in the InvoiceTicket relation when it needs to go to the InvoiceTicket.Ticket relation
##Files https://gist.github.com/hodaddy/8738681
If you want to select columns from related tables like this you have to join the tables manually - doing with() does not work for this. Also, when selecting from another table you need to add the other table in front of the column, i.e. get(['other_table.column'])
anlutro, do mean something like this?
That should work, yes. You can also replace DB::table(..)->
with Model::
How would I specify ('invoice AS i') if I switch DB::table(..)-> with Model:: ?
hodaddy said:
How would I specify ('invoice AS i') if I switch DB::table(..)-> with Model:: ?
have you try Invoice:: ?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community