mercredi 18 janvier 2017

Accessing desired array of data to display and order it from highest to lowest

Anyone could help me accessing the rows or right id in particular from database?Below is my sample code that displays the data but it is not what i am expected because the result is a collection that repeat four times instead of a single collection. My aim is to get the single collection and reorder its scholar_GPA value from highest to lowest. Hope this explains well my problem.

scholar database 
scholar_id  scholar_GPA
1            89
2            83
3            93
4            90
5            96
6            85         
7            87
$scholar_id = ['4','1','7'];

dd($scholar_id) Note: The return of this dd is the value ID of array:3 above .

//result
array:3 [▼
0 => 4
1 => 1
2 => 7
]

$count = 0;

foreach ($scholar_id as $id) {

        $ship_gpa_from = $request['ship_gpa_from'];
        $ship_gpa_to = $request['ship_gpa_to'];

        $scholars = (new Scholar)->newQuery()->select($id);

    Note:This query with the value id 4,1,7 should be reorder 
    from the highest to lowest according to scholar_GPA


        $scholars->whereBetween(DB::raw('scholar_GPA'),array($ship_gpa_from,$ship_gpa_to))->orderBy('scholar_GPA','DESC');  
        $scholars[] = $scholars->get();
       Note: See the dd of $scholars[] below


$count++;
}

dd($scholars);array:4 [▼
  0 => Collection {#279 ▼
    #items: array:7 [▼
      0 => Scholar {#282 ▶}
      1 => Scholar {#281 ▶}
      2 => Scholar {#287 ▶}
    ]
  }
  1 => Collection {#290 ▼
    #items: array:7 [▼
      0 => Scholar {#298 ▶}
      1 => Scholar {#299 ▶}
      2 => Scholar {#300 ▶}
    ]
  }
  2 => Collection {#286 ▼
    #items: array:7 [▼
      0 => Scholar {#306 ▶}
      1 => Scholar {#307 ▶}
      2 => Scholar {#308 ▶}
    ]
  }
]



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jvSuia
via IFTTT

Aucun commentaire:

Enregistrer un commentaire