문제

데이터베이스의 항목이 있는 기존 배열 변수에 새 배열 항목을 푸시하려고 합니다.내가 하고 싶은 것은 이 배열 끝에 '기타'라는 새 항목을 추가하고 데이터베이스의 모든 항목으로 구성된 보기에서 선택 드롭다운으로 표시하는 것입니다. 그리고 이 끝에서 '기타' 항목을 선택합니다. 컨트롤러에 수동으로 추가했습니다.

내가 시도한 것은 다음과 같습니다.

    $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;
    }

그게 뭐라고 했어?

처리되지 않은 예외

메시지:

객체가 아닌 위치의 속성을 가져오려는 중:

C : xampp htdocs khelkheladi khelkheladi application 컨트롤러 register.php 104

내 데이터베이스에는 대회에 이런 유형의 열 구조가 있습니다.

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

주어진 순서대로.

내가 하려는 것은 실제로 데이터베이스에 항목을 삽입하지 않고 보기의 선택 태그에 있는 다른 선택 항목을 정적으로 표시하는 것입니다.새 항목을 실제로 데이터베이스에 삽입하지 않고 보기에만 표시하려면 어떻게 삽입합니까?

이전에 데이터베이스 항목을 검색하여 얻은 출력은 다음과 같습니다.

<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> 
도움이 되었습니까?

해결책

이를 수행하는 "깨끗한" 방법은 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';

다른 팁

배열의 마지막 요소에 비 객체를 추가하기 때문입니다.

여기에서는 name 속성이있는 객체 배열을 가져옵니다

$competition_all = Competition::all();
.

여기에서는 객체 배열의 마지막 요소에 키=> 값 쌍을 추가합니다

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

여기에서는 객체의 배열을 걷고 마지막 요소에 관해서는 "$ competition_games-> name"은 이름 속성이 없습니다

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);
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top