I am trying to apply where condition on joined table, but I'm getting this error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cts.plant' doesn't exist (SQL: select * from
tbl_complaintwhere exists (select * fromtbl_plantwheretbl_complaint.made_in_plant=tbl_plant.plant_idandsap_code= 99999) order bycreateddesc limit 50 offset 0)
My model classes looks something like this:
Main Model
class Complaint extends Model {
protected $connection= 'first';
protected $table = 'complaint';
protected $primaryKey = 'complaint_id';
public function customerPlant() {
return $this->hasOne(Plant::class, 'plant_id', 'customer_plant_id')
->select('plant_id', 'sap_code', 'plant_name');
}
}
Connected Model
class Plant extends Model {
protected $connection= 'second';
protected $table = 'plant';
protected $primaryKey = 'plant_id';
public function getKeyName() {
return 'plant_id';
}
}
Data retrieval:
$query = Complaint::with([
'madeInPlantId']
$query = Complaint::whereHas('madeInPlant', function($query){
$query->where('sap_code','=','99999');
});
$query->get();
I think that problem is I'm not specifying that connected table is in another database.
protected $table = 'database1.plant';- Joe