I'm using Laravel 5.2, I'm trying to 'join' n to m with 'With' from 'Eloquent' but it does not return any value even though they exist in the database.
Class Person:
class Pessoa extends Authenticatable {
const CREATED_AT = null;
const UPDATED_AT = null;
protected $table = 'pessoa';
public $primaryKey = 'idPessoa';
public $incrementing = false;
public function Perfis(){
return $this->hasMany(Perfil::class, 'idPessoa', 'idPessoa');
}
}
class Perfil extends Model {
const CREATED_AT = null;
const UPDATED_AT = null;
protected $table = 'perfil';
public $primaryKey = 'idPerfil';
public $incrementing = false;
}
Requisition Used:
dd( Pessoa::with("Perfis")->has("Perfis")->first()->Perfis );
Result:
Collection {#341 ▼
#items: []
}
I used 'DB :: enableQueryLog ();' to get the SQLs used.
array:2 [▼
0 => array:3 [▼
"query" => "select * from "pessoa" where exists (select * from "perfil" where "perfil"."idPessoa" = "pessoa"."idPessoa") limit 1"
"bindings" => []
"time" => 2.42
]
1 => array:3 [▼
"query" => "select * from "perfil" where "perfil"."idPessoa" in (?)"
"bindings" => array:1 [
"0"=>"1"
]
"time" => 1.33
]
]
If I run the Second SQL I get the Profiles list correctly. What will be the problem then?