uksarkar
6 months ago

How to check a specific data exists on a relationship?

Posted 6 months ago by uksarkar

I have User, Shop and Product model on my app. And relationship like this:

User

    public function products(){
        return $this->hasMany("App\Product");
    }
    public function shops(){
        return $this->hasMany("App\Shop");
    } 

Shop

    public function user(){
        return $this->belongsTo("App\User");
    }
    public function products(){
        return $this->belongsToMany("App\Product");
    }

Product

    public function user(){
        return $this->belongsTo('App\User');
    }
    public function shops(){
        return $this->belongsToMany("App\Shop");
    }

So, user can add Products and Shops and then attach a product to many shop. Now I can get all of the attached shops by $product->shops on product page and users all shop by auth()->user()->shops . But how can I get all of the shops that is not attached with this product yet.

Please sign in or create an account to participate in this conversation.