Learning

If its Laravel or a simple MYSQL a query/fetch of inner join table, return’s id of joined table, but you don’t want that, how to fix?

Hello,

First sorry for such long question in the heading. Will start with small story and then right to the solution fix I found.

Facing issue of getting id of Joined table in my current laravel project, where I need a Id column value from the Main table not the one which I used to inner join for the records to pull in.

Here is my existing query of Laravel returning t he id of customer table where I was looking id of Bill’s Table:

$data = Bill::join('customers', 'customers.id', '=', 'bills.customer_id')
                ->where('bills.store_id', 'LIKE', '%'. $storeId . '%')
                ->orderBy('bills.id', 'desc')
                ->get(['bills.*','customers.*', ]);

So the right solution is at the last line in the above code, please carefully see the get() function array parameters; I switched them, so it finally returns the id values of bills not of customer table. (as customer table also id column which matches with bill’s table column)

$data = Bill::join('customers', 'customers.id', '=', 'bills.customer_id')
                ->where('bills.store_id', 'LIKE', '%'. $storeId . '%')
                ->orderBy('bills.id', 'desc')
                ->get(['customers.*', 'bills.*']);

This results be the records with bills id not customer’s id in laravel elqouent or mysql inner join.

Thanks for reading and learning.

Happy learning.

admin

Recent Posts

The Ultimate Guide to Effective Meetings

In today’s fast-paced work environment, meetings are essential but can often feel unproductive. However, by…

2 months ago

From Mine to Market: How Gold Is Mined, Refined, and Sold in Shops

Gold is one of the most coveted precious metals in the world, adored for its…

3 months ago

The Historical Journey of Gold: From Ancient Civilizations to Modern Times

Gold, the shimmering metal synonymous with wealth, power, and beauty, has been deeply intertwined with…

3 months ago

How to Onboard an Intern in Small, Individual-Based Company

How to Onboard an Intern in a Small, Individual-Based Company Hiring an intern can be…

4 months ago