2013-10-15 9 views
8

लार्वेल एक बहुत अच्छे PHP ढांचे की तरह दिखता है, जो एक अच्छा ओआरएम (वालोकेंट) के साथ बंडल होता है। हालांकि, लार्वाले डॉक्स कुछ कम हैं। दस्तावेज़ में केवल मूल सामग्री मौजूद है।लैरवेल एलोक्वेंट और जटिल रिश्तों

वैसे भी, जब मुझे 2 से अधिक मॉडल फैलते हैं तो मुझे एलोक्वेंट और मॉडल संबंधों की बात आती है।

उदाहरण के लिए, मेरे पास निम्न परिदृश्य है। users, locations, users_locations, packages:

मैं चार डेटाबेस तालिकाओं अर्थात् है। और मॉडल/तालिकाओं के बीच संबंध निम्नानुसार हैं:

उपयोगकर्ता कई स्थान और इसके विपरीत हो सकता है। एक स्थान में कई पैकेज हो सकते हैं।

//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'); 
} 

मुझे क्या करना चाहते हैं:

और मेरे इसी मॉडल रिश्तों इस प्रकार हैं?: मैं सभी संकुल उपयोगकर्ता से संबंधित होना चाहता हूं। उपयोगकर्ता स्थान से संबंधित है, और पैकेज भी स्थानों से संबंधित हैं। इसलिए उपयोगकर्ता से संबंधित सभी स्थानों से, मैं उन पैकेजों को पुनर्प्राप्त करना चाहता हूं जो उपयोगकर्ता के उन स्थानों से संबंधित हैं। मैं भी परिणाम को अंकनित करना चाहता हूं।

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

समस्या है, इसके बाद के संस्करण के साथ, मैं पैकेज पर पृष्ठांकन लागू नहीं कर सकते:

मैं निम्नलिखित की कोशिश की है। क्या किसी को पता है कि इसे ठीक से और कुशल तरीके से कैसे करना है? या यह सिर्फ संभव नहीं है?

उत्तर

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

इसके बजाय 'पेजिनेट ($ page_size) 'कर सकता था। क्या वाक्प्रचार वाला एक लाइन कोड है जहां मुझे उपर्युक्त परिणाम मिल सकते हैं? – WebNovice

+0

पेजिनेट() के बारे में टिप के लिए धन्यवाद - मैं पूरी तरह से इसके बारे में भूल गया। भाषण के लिए मुझे नहीं लगता कि आप इसे अभी तक एक स्वाइप के साथ कर सकते हैं, हालांकि भविष्य में यह देखना बहुत ही सुरुचिपूर्ण होगा। अंत में, आप हमेशा फ्लुएंट का उपयोग कर सकते हैं और अपनी इच्छानुसार टेबल में शामिल हो सकते हैं। –

संबंधित मुद्दे