mercredi 20 novembre 2019

Get all records based on Model type in Polymorphic Relations

I have 3 Models and the related tables: Firm, SaleFirm, PartnerFirm.

In the Firms Table i want to store general informations about firms like name, bank account, ID and stuffs like this. In the other two tables i will store different informations based on the firm's type ( sale firm or partner firm ).

Beacuse of this logic, i need a polymorphic one-to-one relationship between this tables.

In my Firms Table i have this 2 columns: firmable_id and firmable_type.

The relationship between Firm Model and SaleFirm/PartnerFirm Model is this:

class Firm extends Model {
        public function firmable()
        {
                return $this->morphTo();
        }
}
class PartnerFirm extends Model {
        public function firm()
        {
                return $this->morphOne(Firm::class, 'firmable');
        }
}

My questions is this: How can i get all records based on firmable_type, so based on the Model name? Can i do this using the relationship or do i need to do something like this: Firm::where('firmable_type','path\to\PartnerFirm')->get()?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire