2013-10-15 26 views
8

laravel sembra un framework PHP molto bello, in bundle con un buon ORM (Eloquente). Tuttavia, i documenti di laravel sono alcuni che mancano. Solo le cose di base sono presenti nel doc.laravel eloquente e relazioni complesse

In ogni caso, ho un problema quando si tratta di Eloquente e le relazioni del modello quando si estende su più di 2 modelli.

Per esempio, ho il seguente scenario.

Ho quattro tabelle del database e cioè: users, locations, users_locations, packages. E la relazione tra il modello/le tabelle è la seguente:

L'utente può appartenere a più località e viceversa. Una posizione può avere molti pacchetti.

E i miei corrispondenti relazioni modello sono i seguenti:

//User Model: 
public function locations(){ 
    return $this->belongsToMany('Location', 'users_locations', 'user_id', 'location_id'); 
} 

//Location Model: 
public function users(){ 
    return $this->belongsToMany('User', 'users_locations', 'location_id', 'user_id'); 
} 
public function packages(){ 
    return $this->hasMany('Package', 'location_id'); 
} 

//Package Model: 
public function location(){ 
    return $this->belongsTo('Location', 'location_id'); 
} 

Cosa voglio fare?: Voglio che tutti i pacchetti appartengano a un utente. Un utente appartiene a posizioni e i pacchetti appartengono anche alle posizioni. Quindi, da tutti i luoghi che appartengono all'utente, voglio recuperare i pacchetti che appartengono a quelle posizioni dell'utente. Voglio anche che il set di risultati sia impaginato.

Ho provato quanto segue:

//get the logged in user ID 
$userId = Auth::user()->id 
//first get all the locations of the user 
$locations= User::with('locations')->find($userId)->locations; 
//declare an empty array to store the packages 
$packages = array(); 
//now loop through the locations 
foreach($locations as $location){ 
    //since each location can have many packages, we also have to loop through the packages 
    foreach($location->packages as $package){ 
     //store the plan in the array 
     $packages[] = $package; 
    } 
} 
//ok now we got the list of packages 
return $packages; 

Il problema è, con quanto sopra, non posso implementare l'impaginazione sulle confezioni. Qualcuno sa come farlo correttamente e in modo efficiente usando Eloquent? O semplicemente non è possibile?

risposta

5
//get the logged in user ID 
$userId = Auth::user()->id 
//first get all the locations of the user 
$locations= User::with('locations')->find($userId)->locations; 


/* perhaps you can alternatively use lists() function to get the ids 
something like: $loc_ids = DB::table('locations')->where('user_id',$userId)->lists('id'); */ 
$loc_ids = array(); 
foreach($locations as $location) 
{ 
    $loc_ids[] = $location->id; 
} 

$packages = Package::whereIn('location_id', $loc_ids)->skip($offset)->take($page_size)->get(); 

return $packages; 
+1

Potrebbe invece "impaginare ($ page_size)". C'è un codice di linea ONE con eloquente in cui potrei ottenere i risultati di cui sopra? – WebNovice

+0

Grazie per il suggerimento su paginate() - me ne sono completamente dimenticato. Per quanto riguarda Eloquent, non penso che tu possa farlo con un colpo ancora, anche se sarebbe molto elegante vederlo in futuro. Alla fine, puoi sempre usare Fluent e unirti ai tavoli come preferisci. –

Problemi correlati