Search code examples
laravelcollectionslaravel-4eloquentmodels

Laravel 4 pushing structurized array to model collection


I have model Users and new custom collection with same fields.

$users = User::all();

$object = new \Illuminate\Database\Eloquent\Collection;
$object->add(
    [
        "name"=>'John',
        "status"=>"pending",
        "created_at"=> "-0001-11-30 00:00:00",
        "updated_at"=> "-0001-11-30 00:00:00",
    ]
);

// Trying to pull new to the main;
$users->push($object);

Everyting is great, but when I'm trying to loop new collection, it is not retrieving pushed data, and throws Undefined Propery.

Any suggestions, please, I'm totally wasted.


Solution

  • You can do it with a new User instance like this :

    $users = User::all();
    
    $newUser = new User([
            "name"=>'John',
            "status"=>"pending",
            "created_at"=> "-0001-11-30 00:00:00",
            "updated_at"=> "-0001-11-30 00:00:00",
        ]);
    
    // Trying to pull new to the main;
    $users->push($newUser);