我正在撰寫一個資料庫查詢,它回傳存盤在一個名為 $store 的變數中的匹配記錄,我想獲取存盤在 $store 變數中的特定記錄的 ID。
$store=DB::table('books')->where('bookName','dummy')->get();
圖書遷移表結構
$table->increments('id');
$table->string('bookName'); //it's uniqe
$table->int('price');
我正在嘗試的是,當我嘗試根據 $store[$store->id]變數獲取特定書籍的 id 時,它會拋出一個名為的錯誤
Propert id doesnot exist on collection instance
uj5u.com熱心網友回復:
它顯示清楚,collection所以你必須通過以下方式呼叫
$store[0]->id
uj5u.com熱心網友回復:
嘗試這個
$store=DB::table('books')->where('bookName','dummy')->get();
foreach($store as $str){
echo $id = $str->id;
}
uj5u.com熱心網友回復:
如果您只需要 id,常用的方法是使用 select。
Model::select('id')->get();
在視圖中,您可以迭代 ModelCollection 并使用$item->id或 '$item['id']'
uj5u.com熱心網友回復:
前例,獲取具有特定價格的第一本書 ID:
$id = $store->where('price', 100)->first()->id;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326506.html
