2016-08-29 2 views
0

Ich habe eine Sammlung in Laravel 5.3, die ich nach einer Abfrage erstellt, diese dd() nur ein Element ist, nicht zu viel, um Spam wollte ...Laravel -> get() Rückkehr null

Collection {#950 
    #items: array:1 [ 
    0 => Callrail {#942 
     #table: "callrails" 
     #appends: [] 
     #with: [] 
     #hidden: [] 
     #casts: [] 
     #connection: null 
     #primaryKey: "id" 
     #keyType: "int" 
     #perPage: 15 
     +incrementing: true 
     +timestamps: true 
     #attributes: array:2 [ 
     "hourofday" => 1 
     "calls" => 2 
     ] 
     #original: array:2 [ 
     "hourofday" => 1 
     "calls" => 2 
     ] 
     #relations: [] 
     #visible: [] 
     #fillable: [] 
     #guarded: [] 
     #dates: [] 
     #dateFormat: null 
     #touches: [] 
     #observables: [] 
     +exists: true 
     +wasRecentlyCreated: false 
     #forceDeleting: false 
     -originalData: [] 
     -updatedData: [] 
     -updating: false 
     -dontKeep: [] 
     -doKeep: [] 
     #dirtyData: [] 
    } 
    ] 
} 

EDIT # 1:

Hier ist der $calls->toJSON() Ausgang:

[ 
    { 
     "hourofday":1, 
     "calls":2 
    }, 
    { 
     "hourofday":15, 
     "calls":1 
    }, 
    { 
     "hourofday":16, 
     "calls":4 
    }, 
    { 
     "hourofday":18, 
     "calls":7 
    }, 
    { 
     "hourofday":19, 
     "calls":2 
    }, 
    { 
     "hourofday":20, 
     "calls":1 
    }, 
    { 
     "hourofday":22, 
     "calls":2 
    } 
] 

das Problem ist, wenn ich versuche zu tun:

$i = 0; 
    while($i != 24) { 
     $response[] = array(
      'name' => date('g A', strtotime('2016-01-01 '.$i.':00:00')), 
      'y' => $calls->whereStrict('hourofday', $i)->get('calls', 0); 
     ); 
     $i++; 
    } 

Jeder Wert im $response hat immer 0 oder null wenn ich in kein Standardwert gesetzt. Die Werte existieren, sind sie in der Sammlung sind, und richtig formatiert sind, aber aus irgendeinem Grund kann ich nicht schnappt sie. Gibt es etwas, das mir fehlt?

Aktuelle Dokumentation:

https://www.laravel.com/docs/5.3/collections#method-get

EDIT # 2:

die Antwort mit Hilfe von @Andrej Ludinovskov gefunden und der richtigen Antwort:

$i = 0; 
while($i != 24) { 
    // You have to get the first item in the array, then you can use it like normal 
    $callCount = $calls->whereStrict('hourofday', $i)->first(); 
    $response[] = array(
     'name' => date('g A', strtotime('2016-01-01 '.$i.':00:00')), 
     'y' => ($callCount?$callCount->calls:0) 
    ); 
    $i++; 
} 

Antwort

0

Sie haben Sammlung von Elementen und es hat Schlüssel von 0 bis n. Der Schlüssel "Anrufe" ist ein Schlüssel eines Gegenstandes dieser Sammlung. Also sollten Sie den Code wie folgt aussehen:

$i = 0; 
while($i != 24) { 
    $item = $calls->whereStrict('hourofday', $i)->get(0, null); 
    $cnt = 0; 
    if ($item != null) { 
     $cnt = $item->calls 
    } 
    $response[] = array(
      'name' => date('g A', strtotime('2016-01-01 '.$i.':00:00')), 
      'y' => $cnt 
     ); 
    $i++; 

}

+0

Das ergibt ein 'Anruf auf eine Elementfunktion in dem() auf integer' Fehler, der noch weniger Sinn macht ... –

+0

Und auch, wenn ich versuchte 'dd ($ calls-> whereStrict ('hourofday', $ i))', es gibt mir diese Sammlung, die in der ursprünglichen Frage ist. Muss ich '-> first()' vor '-> get()'? –

+0

Vars überlappten sich gegenseitig. Überprüfen Sie den aktuellen Code. –