So, I need to do a join between 2 tables - items and categories.
I'm coding in Laravel and here is what I have:
$items = DB::table('items')
->join('categories', 'categories.id', '=', 'items.category_id')
->get();
And then I get some results like these:
{
id: 3,
barcode: "0002",
category_id: "4",
price: 200,
in_use: 1,
serial_number: 1112,
model: "Toshiba",
condition_id: 3,
person_id: 1,
comments: "A monitor that is usually connected to a laptop.",
created_at: "2017-03-28 19:50:02",
updated_at: "2017-03-28 19:50:02",
name: "monitor",
},
{
id: 3,
barcode: "0003",
category_id: "4",
price: 300,
in_use: 1,
serial_number: 11342,
model: "Toshiba",
condition_id: 3,
person_id: 1,
comments: "A monitor that is usually connected to a laptop.",
created_at: "2017-03-28 19:50:02",
updated_at: "2017-03-28 19:50:02",
name: "monitor",
},
Both tables have some fields that have the same name, such as id
created_at
and updated_at
.
The problem is that, because they have the same name, the values of the one table overwrite the values of the other table.
How do I get the 2nd table values to not overwrite the values from the first one when they have the same column name?
Or, even better, how do I get both values back (from both tables)?
Maybe using the AS
keyword somehow?
Thanks for the help.