With do Eloquent is not bringing any value

Asked

Viewed 77 times

0

I am using Laravel 5.2, I am trying to make a 'Join' n to m with the 'With' of 'Eloquent' but it does not return any value, even if they exist in the database.

Class Pessoa:

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 );

Upshot:

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 Second SQL I get the list of Profiles correctly. What will be the problem then?

  • There are some points that are not patterns in Laravel one are the name of the method of the relation that is Profiles and no with you called Profile already ta wrong has to be the same name of the method, and by default it must be all minuscule ... if it managed to solve?

  • I fixed it, but it still doesn’t work, and the funniest thing that if I take the with it brings the values. But doing so every time I run this method will run an SQL that would end the performance.

  • Peter following what you want to do with with + has, could you explain better? Why make a Join is another way? What results you need to bring? can put all classes involved

  • I’m trying to bring all users who have profiles and their respective profiles.

  • Put to me the two tables or tables involved in your question, N:M ta wrong in Laravel for example, so let me see your bank template.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.