第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將 Laravel 請(qǐng)求的對(duì)象從查詢轉(zhuǎn)換為 int

如何將 Laravel 請(qǐng)求的對(duì)象從查詢轉(zhuǎn)換為 int

PHP
不負(fù)相思意 2021-06-30 18:57:37
我正在使用 Laravel 查詢和帶有簡(jiǎn)單查詢的數(shù)組從數(shù)據(jù)庫中獲取整個(gè)對(duì)象(見下圖)public function editgamemenu($id){    $game = Game::find($id);    $genres = Genre::all();    $selectedgenres = DB::table('game_genre')->select('genre_id')->where('game_id','=',$id)->get()->toArray();    returnview('editgametext')>with(compact('game','genres','selectedgenres'));}之后,我使用 foreach 遍歷對(duì)象以嘗試查看對(duì)象的 id 是否在我之前提取的數(shù)組中,但它給了您以下錯(cuò)誤:“類 stdClass 的對(duì)象無法轉(zhuǎn)換為 int”查看代碼:<input type="hidden" value="{{$game->id}}" id="id" name="id"><div class="form-group col-4">  <p><strong>Genres:</strong></p>  <div class="container">  @foreach($genres as $genre)    <span class="genre-list">      <label>{{$genre->name}}</label>      @if(in_array($genre,$selectedgenres))      <input type="checkbox" name="genres[]" value="{{$genre->id}}" checked>      @else      <input type="checkbox" name="genres[]" value="{{$genre->id}}">      @endif    </span>  @endforeach我已經(jīng)嘗試將 $genre->id 轉(zhuǎn)換為 int ,但它不起作用,還有更多的東西。幫助將不勝感激。謝謝
查看完整描述

3 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊

改變


$selectedgenres = DB::table('game_genre')

    ->select('genre_id')

    ->where('game_id','=',$id)

    ->get()

    ->toArray();

對(duì)此


$selectedgenres = DB::table('game_genre')

    ->select('genre_id')

    ->where('game_id','=',$id)

    ->get()

    ->pluck('genre_id')

    ->toArray();

注意 ->pluck('genre_id')


如果沒有 pluck,您將擁有一個(gè)對(duì)象數(shù)組。


編輯:正如@quickSwap 注意到的,在您的刀片文件中,您還需要更改$genre為$genre->id


@if(in_array($genre->id, $selectedgenres))

    ....

@endif

編輯2:您還可以利用 Collection


$selectedgenres = DB::table('game_genre')

    ->where('game_id','=',$id)

    ->pluck('genre_id'); // returns a collection

在你的刀片中


@if($selectedgenres->contains($genre->id))


查看完整回答
反對(duì) 回復(fù) 2021-07-09
  • 3 回答
  • 0 關(guān)注
  • 392 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)