Push Array in Laravel

I am trying to push a new array element into an existing array variable that has elements from the database. What I want to do is add a new element with the name “Other” at the end of this array and display it as select in the drop-down list that consists of all the elements from the database and at the end of it select the “Other” element, which I manually added to 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 he said looks like this

Unhandled exception

Message:

Attempting to get a non-object property Location:

C: \ XAMPP \ HTDOCS \ khelkheladi \ khelkheladi \ applications \ Controllers \ register.php on line 104

In my database, Competition has this type of column structure.

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

in the given order.

, , - , select. , , ?

, , ,

<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> 

,

<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> 
+5
2

"" Competition .

, , :

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

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

, - .

, name

$competition_all = Competition::all();

= >

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

, , "$ contest_games- > "

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

- stdclass :

$newCompete = new StdClass();
$newCompete->name = 'Others';
array_push($competition_all, $newCompete);
+4

All Articles