जवाबों:
बस orderBy()
जरूरत पड़ने पर कई बार आह्वान करें । उदाहरण के लिए:
User::orderBy('name', 'DESC')
->orderBy('email', 'ASC')
->get();
निम्नलिखित क्वेरी का उत्पादन करता है:
SELECT * FROM `users` ORDER BY `name` DESC, `email` ASC
User::orderBy(array('name'=>'desc', 'email'=>'asc'))
$user->orders = array(array('column' => 'name', 'direction' => 'desc'), array('column' => 'email', 'direction' => 'asc'));
get
या first
), तो बस orderBy
उस पर कॉल करें । Else, nope।
आप कर सकते हैं जैसा कि @rmobis ने अपने उत्तर में निर्दिष्ट किया है, [इसमें कुछ और जोड़ना]
order by
दो बार उपयोग करना :
MyTable::orderBy('coloumn1', 'DESC')
->orderBy('coloumn2', 'ASC')
->get();
और ऐसा करने का दूसरा तरीका है,
का उपयोग कर raw order by
:
MyTable::orderByRaw("coloumn1 DESC, coloumn2 ASC");
->get();
दोनों एक ही क्वेरी को फॉलो करेंगे,
SELECT * FROM `my_tables` ORDER BY `coloumn1` DESC, `coloumn2` ASC
जैसा कि @rmobis पहले उत्तर की टिप्पणी में निर्दिष्ट है, आप इस तरह से कॉलम द्वारा ऑर्डर करने के लिए एक सरणी की तरह पास कर सकते हैं ,
$myTable->orders = array(
array('column' => 'coloumn1', 'direction' => 'desc'),
array('column' => 'coloumn2', 'direction' => 'asc')
);
इसे करने का एक और तरीका iterate
लूप में है,
$query = DB::table('my_tables');
foreach ($request->get('order_by_columns') as $column => $direction) {
$query->orderBy($column, $direction);
}
$results = $query->get();
आशा करता हूँ की ये काम करेगा :)
यहाँ एक और चकमा दिया गया है जो मैं अपने आधार भंडार वर्ग के लिए आया हूँ जहाँ मुझे स्तंभों की संख्या के आधार पर ऑर्डर करने की आवश्यकता है:
public function findAll(array $where = [], array $with = [], array $orderBy = [], int $limit = 10)
{
$result = $this->model->with($with);
$dataSet = $result->where($where)
// Conditionally use $orderBy if not empty
->when(!empty($orderBy), function ($query) use ($orderBy) {
// Break $orderBy into pairs
$pairs = array_chunk($orderBy, 2);
// Iterate over the pairs
foreach ($pairs as $pair) {
// Use the 'splat' to turn the pair into two arguments
$query->orderBy(...$pair);
}
})
->paginate($limit)
->appends(Input::except('page'));
return $dataSet;
}
अब, आप अपना कॉल इस तरह कर सकते हैं:
$allUsers = $userRepository->findAll([], [], ['name', 'DESC', 'email', 'ASC'], 100);
User::orderBy('name', 'DESC') ->orderBy('email', 'ASC') ->get();