Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Hi I am using Laravel 8 and have two tables Products and Categories. Both tables have name column. when i run the following query as join it returns categories.name but not products.name. where am I making mistake.
return $product = DB::table('categories')
->join('products', 'products.category_id','=', 'categories.id')
->select('products.name','categories.name','products.price','products.description','categories.id','products.category_id')
->get();
return view('products.single',compact('product'));
Result of the above query is
[{“name":"Category First","price":"742.00","description":"Distinctio Sapiente","id":1,"category_id":1},{"name":"Category First","price":"275.00","description":"Nobis velit totam a","id":1,"category_id":1},{"name":"Category First","price":"242.00","description":"Duis incidunt qui c","id":1,"category_id":1}]
–
–
–
When two tables have a column with the same name you have to use an alias, something like this:
->select('products.name','categories.name AS category','products.price','products.description','categories.id','products.category_id')
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.