我的功能有問題。我請求從數據庫中選擇,選擇是正常的,并且有第二個代碼應該price從結果中獲取列的值,但問題是我得到一個錯誤結果:Trying to get property of non-object.我的代碼:$betitem = \DB::table('items')->where('status', 0)->where('price', '>=', 10)->orderByRaw('RAND()')->take(1)->get();
$green_tickets = $betitem->price;如果寫var_dump($betitem[0]); exit;我收到:object(stdClass)#584 (13) { ["id"]=> int(548) ["assetid"]=> string(11) "18235855849" ["market_hash_name"]=> string(15) "Staff of Gun-Yu" ["classid"]=> string(231) "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KW1Zwwo4NUX4oFJZEHLbXK9QlSPcU_phVWSVXvTO2j0IDeXFN_IB1ovbOrLDhp3v7HYylD4OOhkYGbmPm7PrTfnW5I1854hO7-_IH4h0agqh8DJDyiZNnLbAE8M13Q-Ae4wrq7g5Pq7cufnCRm7nZ3tCyPlhSyhx1IabZrjPKaQVqAR_se2_6rU3g" ["price"]=> float(26.4) ["steamid"]=> string(1) "1" ["type"]=> string(4) "card" ["bot"]=> string(1) "1" ["status"]=> int(0) ["created_at"]=> string(19) "2020-02-27 01:47:12" ["updated_at"]=> string(19) "2020-03-14 17:41:25" ["is_withdraw"]=> int(0) ["is_raffling"]=> int(0) }我如何理解問題的出現是因為結果是在數組中接收的。但是如何修復這個錯誤并從price列中獲取結果呢?
3 回答

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
由于$betitem
數組中有一個對象,您可以這樣做
$green_tickets = $betitem[0]->price;
或者您可以使用該first()
方法獲取與您的條件匹配的第一行。
$betitem = \DB::table('items')->where('status', 0)->where('price', '>=', 10)->orderByRaw('RAND()')->take(1)->first(); $green_tickets = $betitem->price;
另外,我可以看到一個額外的\
before DB
。您可以use DB;
在聲明類之前添加它應該可以正常運行。確保此DB
別名存在于您的config/ app.php
.

慕的地8271018
TA貢獻1796條經驗 獲得超4個贊
在這種情況下,使用 first() 而不是 take(1)->get()
$betitem = \DB::table('items')->where('status', 0)->where('price', '>=', 10)->orderByRaw('RAND()')->first(); $green_tickets = $betitem->price;

holdtom
TA貢獻1805條經驗 獲得超10個贊
嘗試這個
$betitem = items::where('status',0)->andWhere('price', '>=', 10)->orderBy('id', 'ASC')->take(1)->get();
$green_tickets = $betitem->price;
- 3 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消