2017-03-03 11 views
1

質問:なぜ古いアプローチは古いアプローチを結果参照正しい値を返し、新しいHOMのアプローチは、全体のコレクションを返すのですか?Laravel 5.4高次メッセージング

ロールモデル

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

コントローラ

$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()); 

私は私が得る古いものを使用している場合、それは、私の全体のコレクションを返しメッセージング高い順に新しいアプローチを実装する場合正しい結果。

結果、古いアプローチ

文字列(11) "アプリケーション"

文字列(6) "システム"

文字列(7) "ネットワーク"

文字列( 7) "マネージャー"

結果新しいアプローチコレクションの

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

あなたの質問は何ですか? – kaufmanu

答えて

3

eachだけ繰り返し処理は、それが実際には何も返しません。あなたはそれが反復ごとにgetName()の値を返すようにしたいなら、あなたは、マップなどを使用することができます

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