Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

imranicp1's avatar

Distinguish model(record) which doesnt have a relationship(record in another table).

I have 3 tables in database, Market, Marketbrands, and Brands, there are one-to-many relationship between Market and Marketbrands and Brands and Marketbrands, i want to display data from these 3 tables to a view. Brands has some records, each record in brands can have a relationship with Marketbrands table or not, depending on the Brand.

I retrieved the data from database, populated the dropbox for each brand from marketbrands, my question is , for those brands which dont have a record (relationship) in/(with) marketbrands. Some code to explain it further. marketBrands() and market() are the relationships. i cant seem to separate the brands whose records are not present in Marketbrands. any help will be appreciated.

 $brand_set=Brand::paginate(50);
        foreach ($brand_set as $br){
            echo $br->marketBrands()->get();
            echo "<br>";
            foreach ($br->marketBrands()->get() as $mb){
                echo $br->marketBrands()->count();
                echo "<br>";
                foreach ($market_set as $market){

                    if ($br->marketBrands()->get()===0){
                    echo "You are here" . $br->brand_id;
                        echo "<br>";
                    }


                    if ($mb->market()->first()->market_id === $market->market_id)
                    {
                        echo $br->brand_id." and ".$br->brand_name ." and ". $mb->market()->first()->market_name;
                        echo"<br>";
                    }


                    }

                }
            }
0 likes
0 replies

Please or to participate in this conversation.