Eli
Eli

Reputation: 1276

How to get 2 primary ids of 2 tables using join query in Laravel 5.2?

I have a query that will get the data from a joined tables. I successfully fetched the data from the 2 tables but for a long time, I did not notice that only one primary id of a certain table has been returned. I made adjustments but still never figure it out. What would I do? Please help. Thanks a lot guys. Here is my code.

$purchase = Purchase::where('purchases.purchase_order_id', $id)
                        ->join('products', 'purchases.product_id', '=', 'products.id')
                        ->select('purchases.*', 'products.*')
                        ->get();

It only returns the primary id of a product, primary id of the table purchases is not included. What is the problem of the query above?

Upvotes: 0

Views: 94

Answers (2)

Madhav Shandilya
Madhav Shandilya

Reputation: 1

This query returns the IDs of Survey Table as surveys_id as well as IDs of industries table along with all other data:

surveydata = Survey::select(
    'surveys.*', 
    'surveys.id as surveys_id',
    'industries.*', 
    'industries.id as industries_id'
)->where('surveys.active_status', '=','1')
-> join (
    'industries', 
    'industries.id', 
    '=',
    'surveys.survey_industry_id'
)->orderBy('surveys.id','desc')
->paginate(12);

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 17668

You can use select as:

->select('purchases.*', 'purchases.id as purchase_id', 'products.*', 'products.id as product_id')

Upvotes: 1

Related Questions