I want to fetch all invoices a user has raised using the Eloquent ORM.
At the moment, I'm fetching the invoice.id
from a join table UsersInvoices by the user.id
and then looping through and fetching the invoices from the Invoices table based on the id.
$user = Sentry::getUser();
$invoiceIDs = UsersInvoice::where('user_id', '=', $user->id)->get()->toArray();
$invoices = array();
foreach ($invoiceIDs as $key => $row) {
array_push($invoices, Invoice::find($row['invoice_id']));
}
$this->data['title'] = 'Invoices';
$this->data['invoices'] = $invoices;
/** render the template */
App::render('@invoices/pending/index.twig', $this->data);
But I think this could/should be handled by the ORM by modelling the relationship between user and invoices.
It looks as though I should be able to manage this like so by simply posting the user.id
into the invoices table as a foreign key - Maybe I don't need a join table as there is nothing that is unique to the relationship that needs to be stored... ?