Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Push in Laravel

I am trying to push new array item into existing array variable that has items from database. What I want to do is add a new item named 'Others' at the end of this array and display it as select drop down in view which consists of all the items from database and at the end of this select the 'Others' item that I manually added in my controller.

Here is what I tried to do:

    $competition_all = Competition::all();
    $newCompete = array('name'=>'Others');
    array_push($competition_all, $newCompete);

    $this->competition_games = array('Competition');

    foreach ($competition_all as $competition_games) {
        $this->competition_games[$competition_games->name] = $competition_games->name;
    }

what it said is like this

Unhandled Exception

Message:

Trying to get property of non-object Location:

C:\xampp\htdocs\khelkheladi\khelkheladi\application\controllers\register.php on line 104

In my database the Competition has this type of column structure

->id
->year
->place
->name
->created_at
->updated_at

in given order.

What I'm trying to do is without actually inserting an item in database, just statically show the others select item in select tag in view. How do I insert such new item without actually inserting it to database but to display in view only?

The output what i'm getting before by just retrieving database item is like this

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
</select> 

what I like to do is like this

<select>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
  <option value="4">Value 4</option>
  <option value="5">Others</option>
</select> 
like image 605
monk Avatar asked Oct 02 '12 06:10

monk


2 Answers

That's because you're adding a non-object to the last element of the array.

Here i suppose you get an array of objects with the name property

$competition_all = Competition::all();

Here you add a key=>value pair to the last element of the objects array

$newCompete = array('name'=>'Others');
array_push($competition_all, $newCompete);

Here you walk through the array of objects and when it comes to the last element the "$competition_games->name" has no name property

foreach ($competition_all as $competition_games) {
            $this->competition_games[$competition_games->name] = $competition_games->name;
        }

Try something like including a stdclass for it like :

$newCompete = new StdClass();
$newCompete->name = 'Others';
array_push($competition_all, $newCompete);
like image 168
Tony Avatar answered Sep 25 '22 14:09

Tony


The "clean" way to do it would be to create an instance of Competition without committing it to the database, and repeat once more your cycle with the extra instance.

However, here you appear to be just producing a list, so it ought to be enough to do a much quicker addition to the final list:

// Empty array
$this->competition_games = [ 0 => 'Others' ];

foreach (Competition::all() as $game) {
    $this->competition_games[$game->id] = $game->name;
}

Using 0 (or -1) as the nonexistent Id.

like image 40
LSerni Avatar answered Sep 25 '22 14:09

LSerni