0

I'm using eloquent polymorphic relationships that is awesome to manage pivot table between diferent models and other model called company.

I have a pivot table that contains the following structure: enter image description here

I have to make a join query between vehicle table and pivot table using model_id and model_type using eloquent query builder. But when I do:

$builder->join('pivot_table', function($join){
        $join->on('vehicle.id','=','pivot_table.model_id')
            ->on('pivot.model_type', Vehiculo::class  );
    })->select('vehicle.*',pivot_table.*)->get();

this code don't return any result. But if I change the second on clause to:

    $builder->join('pivot_table', function($join){
        $join->on('vehicle.id','=','pivot_table.model_id')
            ->on('pivot.model_type', 'like' , '%Vehiculo%');
    })->select('vehicle.*',pivot_table.*)->get();

this code runs correctly and returns the results what I want, but I think that is the wrong way to obtain the results.

Somebody knows if there is a way make run the first code?

thanks for the responses.

bcg
  • 261
  • 1
  • 4
  • 26

1 Answers1

3

Ok. I solved. The solution is changing the second on to where

$builder->join('pivot_table', function($join){
        $join->on('vehicle.id','=','pivot_table.model_id')
            ->where('pivot.model_type' , Vehiculo::class);
    })->select('vehicle.*',pivot_table.*)->get();
bcg
  • 261
  • 1
  • 4
  • 26