2014-09-11 20 views
5

Ciao voglio recuperare il mio projects tenuto in un db, che sono di proprietà di un auth user che crea anche clients (hanno molti progetti e attività) e tasks (appartiene a una progetto e compiti e utente).laravel rapporto eloquenti con e dove la clausola in base a colonna estera

Voglio recuperare tutti i compiti che non sono contrassegnati come chiuso nella tabella di stato, so che l'id di questo è 2 e posso recuperare questo come modo:

public function getOpenProjects() { 

    return \Project::with(['clients', 'tasks', 'status']) 
     ->where('status_id', '!=', '2') 
     ->whereUserId(Auth::user()->id) 
     ->get(); 
} 

Ma come posso cambiare questa interrogare una colonna nella tabella degli stati, cioè la colonna del nome in quella tabella?

risposta

16

Si può provare questo:

$value = 'someName'; 
Project::with(['clients', 'tasks', 'status' => function($q) use($value) { 
    // Query the name field in status table 
    $q->where('name', '=', $value); // '=' is optional 
}]) 
->where('status_id', '!=', '2') 
->whereUserId(Auth::user()->id) 
->get(); 

Inoltre si può provare questo (Si preleverà record solo se i query rendimenti name che volete, altrimenti nessuno):

$value = 'someName'; 
Project::with(['clients', 'tasks', 'status']) 
     ->whereHas('status', function($q) use($value) { 
     // Query the name field in status table 
     $q->where('name', '=', $value); // '=' is optional 
}) 
->where('status_id', '!=', '2') 
->whereUserId(Auth::user()->id) 
->get(); 
+0

Voglio interrogare il campo nome tabella stato non la tabella progetti – 001221

+0

Sì, questo è '$ q-> dove ('name', '=', $ value)' all'interno di 'closure' che è il valore di' status'. –

+0

perché recupera lo stato con null? – 001221

0

si può provare questo :

Project::with(['clients', 'tasks' => function($q) use($value) { 
// Query the name field in status table 
    $q->where('status_id', '!=', '2'); 
}]) 
->whereUserId(Auth::user()->id) 
->get(); 
Problemi correlati