I am new to PDO PHP and I'm confident that I'm not writing it in an efficient manner. I'm constantly struggling with how to get object data from within a collection and the way I'm doing it is ugly.
For example, I have two tables work_orders and aircraft. I want to get the aircraft registration from the aircraft table, through the use of my aircraft_id field located in my work_orders table.
Here is what I have in my Controller:
// Temporary injection
$order_number = '502';
// Get the aircraft_id that corresponds to the work order supplied
$aircraft_id = $workorder->select('aircraft_id')
->where('id', $order_number)
->get();
// Assigned the aircraft_id to a variable
$aircraft_id = $aircraft_id[0]->aircraft_id;
// Use the aircraft_id to find get the aircraft data
$aircraft = $aircraft->findOrFail($aircraft_id);
I realize that this has join() written all over it, and I did have it joined, but I got here by trying to solve my problem...if that makes sense.
The part that's getting me all the time is I want to simply type $aircraft_id->aircraft_id and I end up hitting the same wall.
Is there a better way to structure my query, or is this just the way it is?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire