2017-03-03 5 views
1

Frage: Warum gibt der alte Ansatz den richtigen Wert zurück? Siehe Ergebnis alter Ansatz und der neue HOM-Ansatz gibt die gesamte Sammlung zurück?Laravel 5.4 Nachrichten höherer Ordnung

Role Model

class Role extends Model { 
    public function getName() { 
     return $this->name; 
    } 
} 

-Controller

$roles = Role::all(); // get all roles to test 

// old approach 
$roles->each(function(Role $i) { 
    var_dump($i->getName()); 
}); 

// new approach (HOM) 
var_dump($roles->each->getName()); 

Wenn ich den neuen Ansatz mit höherer Ordnung Messaging implementieren es gibt mir die ganze Sammlung, wenn ich die alte verwende ich bekommen das richtige Ergebnis.

Ergebnis altes Konzept

string (11) "Anwendung"

string (6) "System"

string (7) "Netzwerk"

string (7) "Manager"

Ergebnis neuer Ansatz

object(Illuminate\Database\Eloquent\Collection)#196 (1) { 
    ["items":protected]=> 
    array(4) { 
[0]=> 
object(App\Modules\Role\Role)#197 (24) { 
    ["connection":protected]=> 
    NULL 
    ["table":protected]=> 
    NULL 
    ["primaryKey":protected]=> 
    string(2) "id" 
    ["keyType":protected]=> 
    string(3) "int" 
    ["incrementing"]=> 
    bool(true) 
    ["with":protected]=> 
    array(0) { 
    } 
    ["perPage":protected]=> 
    int(15) 
    ["exists"]=> 
    bool(true) 
    ["wasRecentlyCreated"]=> 
    bool(false) 
    ["attributes":protected]=> 
    array(5) { 
    ["id"]=> 
    int(1) 
    ["name"]=> 
    string(11) "Application" 
    ["description"]=> 
    string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat." 
    ["created_at"]=> 
    string(19) "2017-03-03 11:56:09" 
    ["updated_at"]=> 
    string(19) "2017-03-03 11:56:09" 
    } 
    ["original":protected]=> 
    array(5) { 
    ["id"]=> 
    int(1) 
    ["name"]=> 
    string(11) "Application" 
    ["description"]=> 
    string(91) "Voluptatem similique pariatur iure. Et quaerat possimus laborum non sint aspernatur fugiat." 
    ["created_at"]=> 
    string(19) "2017-03-03 11:56:09" 
    ["updated_at"]=> 
    string(19) "2017-03-03 11:56:09" 
    } 
    ["casts":protected]=> 
    array(0) { 
    } 
    ["dates":protected]=> 
    array(0) { 
    } 
    ["dateFormat":protected]=> 
    NULL 
    ["appends":protected]=> 
    array(0) { 
    } 
    ["events":protected]=> 
    array(0) { 
    } 
    ["observables":protected]=> 
    array(0) { 
    } 
    ["relations":protected]=> 
    array(0) { 
    } 
    ["touches":protected]=> 
    array(0) { 
    } 
    ["timestamps"]=> 
    bool(true) 
    ["hidden":protected]=> 
    array(0) { 
    } 
    ["visible":protected]=> 
    array(0) { 
    } 
    ["fillable":protected]=> 
    array(0) { 
    } 
    ["guarded":protected]=> 
    array(1) { 
    [0]=> 
    string(1) "*" 
    } 
} 
} 
+0

Was ist Ihre Frage? – kaufmanu

Antwort

3

each gerade iteriert über die Sammlung, ist es nicht wirklich etwas zurück. Wenn Sie möchten, dass der Wert getName() für jede Iteration zurückgegeben wird, können Sie die Karte z.

dump($roles->map->getName()); 

Hoffe, das hilft!

Verwandte Themen