2016-09-12 2 views
0

Ich habe folgende Anfrage in Laravel:Wie wird der Schlüssel für das Abfrageergebnis gesetzt?

$name = Place::where("status", "1", function ($query) use ($request) { 

      if (($from = $request->get("from"))) { 
       $query->where('name', 'like', $from . '%'); 
      } 

      if (($to = $request->get("to"))) { 
       $query->where('name', 'like', $to . '%'); 
      } 

     })->orderBy('name', 'desc')->get(); 

I Ergebnis zu erhalten, wie zum Beispiel:

{ 
    "id": 2, 
    "name": "Lena" 
}, 
{ 
    "id": 1, 
    "name": "Baran" 
} 

Wie kann ich Tasten für diese beiden Ergebniszeilen gesetzt? Ich möchte tun:

name_one = { 
     "id": 2, 
     "name": "Lena" 
    }, 
    name_two = { 
     "id": 1, 
     "name": "Baran" 
    } 
+0

$ ss = array ('name_one', 'name_zwei'); $ res = array_combine ($ ss, json_decode ($ ihr_array, wahr)); print_r ($ res); – JYoThI

+0

'$ name' ist Sammlung – Bedouin

+0

sein Aussehen wie JSON Daten, aber es ist nicht gültig JSON – JYoThI

Antwort

0

Sie können

public function getCollectinos() 
    { 
     $collection = collect([ 
      ['id' => '1', 'product' => 'Chair'], 
      ['id' => '2', 'product' => 'Desk'], 
      ['id' => '3', 'product' => 'Bookcase'], 
     ]); 


     $grouped = $collection->groupBy(function ($item, $key) { 
      return 'name_'.$this->number_to_word($item['id']); 
     }); 

     $array = $grouped->toArray(); 
     dd($array); 
    }  



    public function number_to_word($num = '') 
    { 
     $num = (string) ((int) $num); 

     if((int) ($num) && ctype_digit($num)) 
     { 
      $words = array(); 

      $num = str_replace(array(',' , ' ') , '' , trim($num)); 

      $list1 = array('','one','two','three','four','five','six','seven', 
       'eight','nine','ten','eleven','twelve','thirteen','fourteen', 
       'fifteen','sixteen','seventeen','eighteen','nineteen'); 

      $list2 = array('','ten','twenty','thirty','forty','fifty','sixty', 
       'seventy','eighty','ninety','hundred'); 

      $list3 = array('','thousand','million','billion','trillion', 
       'quadrillion','quintillion','sextillion','septillion', 
       'octillion','nonillion','decillion','undecillion', 
       'duodecillion','tredecillion','quattuordecillion', 
       'quindecillion','sexdecillion','septendecillion', 
       'octodecillion','novemdecillion','vigintillion'); 

      $num_length = strlen($num); 
      $levels = (int) (($num_length + 2)/3); 
      $max_length = $levels * 3; 
      $num = substr('00'.$num , -$max_length); 
      $num_levels = str_split($num , 3); 

      foreach($num_levels as $num_part) 
      { 
       $levels--; 
       $hundreds = (int) ($num_part/100); 
       $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ($hundreds == 1 ? '' : 's') . ' ' : ''); 
       $tens  = (int) ($num_part % 100); 
       $singles = ''; 

       if($tens < 20) 
       { 
        $tens = ($tens ? ' ' . $list1[$tens] . ' ' : ''); 
       } 
       else 
       { 
        $tens = (int) ($tens/10); 
        $tens = ' ' . $list2[$tens] . ' '; 
        $singles = (int) ($num_part % 10); 
        $singles = ' ' . $list1[$singles] . ' '; 
       } 
       $words[] = $hundreds . $tens . $singles . (($levels && (int) ($num_part)) ? ' ' . $list3[$levels] . ' ' : ''); 
      } 

      $commas = count($words); 

      if($commas > 1) 
      { 
       $commas = $commas - 1; 
      } 

      $words = implode(', ' , $words); 

      $words = trim(str_replace(' ,' , ',' , $this->trim_all( $words )) , ', '); 
      if($commas) 
      { 
       $words = $this->str_replace_last(',' , ' and' , $words); 
      } 

      return $words; 
     } 
     else if(! ((int) $num)) 
     { 
      return 'Zero'; 
     } 
     return ''; 
    } 


    public function str_replace_last($search , $replace , $str) 
    { 
     if(($pos = strrpos($str , $search)) !== false) { 
      $search_length = strlen($search); 
      $str = substr_replace($str , $replace , $pos , $search_length); 
     } 
     return $str; 
    } 


    public function trim_all($str , $what = NULL , $with = ' ') 
    { 
     if($what === NULL) 
     { 
      // Character  Decimal  Use 
      // "\0"   0   Null Character 
      // "\t"   9   Tab 
      // "\n"   10   New line 
      // "\x0B"   11   Vertical Tab 
      // "\r"   13   New Line in Mac 
      // " "   32   Space 

      $what = "\\x00-\\x20"; //all white-spaces and control chars 
     } 

     return trim(preg_replace("/[".$what."]+/" , $with , $str) , $what); 
    } 

Ausgabe dieser Code versuchen:

enter image description here

0

können Sie eine keyBy('name'); nach dem get() Funktion Lookup keyBy Beispiel unten

$collection = collect([ 
    ['product_id' => 'prod-100', 'name' => 'desk'], 
    ['product_id' => 'prod-200', 'name' => 'chair'], 
]); 

$keyed = $collection->keyBy('product_id'); 

$keyed->all(); 

/* 
    [ 
     'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 
     'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], 
    ] 
*/ 
Verwandte Themen