lundi 21 septembre 2015

How to access only records where relation to other table is available - Laravel

Let me first show you the tables

users_table    
id | name

businesses_table
id | user_id | name

forsale_table
id | business_id | title

So Im using Laravel and to get user's businesses we have to write:

public function businesses() {
        return $this->hasMany('App\Models\Business');
    } 

And the business can have relation to forsale table, to access it we have to write:

public function sale() {
        return $this->hasOne('App\Models\Sale');
    }

but how to access only these businesses where for sale advert is available?

@Edit: I managed to do this:

public function sale() {
        return $this->hasManyThrough('App\Models\Sale', 'App\Models\Business');
}

It works pretty well, but it overrides my description, how can i access both advert description and business description



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire