How to do this with ORM Laravel

0

In my database I have the following tables:

Document (idDoc, title, type)

Project (idProject, name)

DocumentProject (idDP, fk_Documento, fk_Proyecto)

I need to know how to do this with the Laravel ORM in a single consultation.

1.- Filter those documents whose type is 2 (int)

2.- Of the resulting documents, those who have the "Friends" project should be compared.

3.- The query should only give me the documents that have the project "Friends" and type 2.

    
asked by Ccin 12.07.2018 в 18:18
source

1 answer

1

I think that your entire query could be summarized in something like this, assuming that the fields and relationships are as follows:

class Proyecto {

  public function documentos()
  {
     return $this->belongsToMany(App\Documento, 'documento_proyecto')
  }

}


Proyecto::whereHas('documentos', function ($query) {
     $query->where('tipo', 2);
})->where('nombre', 'Amigos')->get();
    
answered by 12.07.2018 в 21:31