vendredi 2 octobre 2020

Join table with its model's default scope in Laravel

Currently, we can join 2 tables like

ModelA::join('table_b', 'table_a.id', '=', 'table_b.a_id');

With this approach the default scopes on model for table_b (ie: ModelB) are not applied on query. Suppose the ModelB has SoftDeletes enabled now the above join won't include whereRaw('table_b.deleted_at IS NULL'). I know i can manualy add this using following code.

ModelA::join('table_b', function($join) {
    $join->('table_a.id', '=', 'table_b.a_id')->whereRaw('table_b.deleted_at IS NULL');
});

I want to know if there is any method to join so that it automatically apply default scope(s) in ModeB. Something like:

ModelA::joinModel(ModelB::Class, 'table_a.id', '=', 'table_b.a_id')


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire