Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel get relation with another relation

SellerController.php

public function products()
    {
        $seller = Auth::user();
        $products = Seller::find($seller->id)->products; //return only products, I need to get category with this relation!!!
        return response()->json(['data' => $products], 200);
    }

This return seller’s products.

Seller.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public function products() {
        return $this->hasMany('App\Models\Products', 'user');
    }

Products.php

function seller()
    {
        return $this->belongsTo('App\Models\Seller', 'user');
    }

    function Category()
    {
        return $this->hasOne('App\Models\Category','cat');
    }

Now I want to return category with products in seller controller, how can I do this?

>Solution :

Use with function

 $products = Seller::findOrFail($seller->id)->products()->with('Category')->get();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading