[英]Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id
[英]laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id
該網站有一個抽獎頁面。 默認情況下,最后一次開獎通過$giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->first();
,一切都很好。 當我嘗試添加另一個抽獎時,不再顯示前一個抽獎並顯示最新的抽獎。
我嘗試像這樣更改$giveaway
(正如我在第一行解釋的那樣):
$giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->take(2)->get();
我收到錯誤PagesController.php line 385: Undefined property: Illuminate\\Database\\Eloquent\\Collection::$id
, 385 line it PagesController.php line 385: Undefined property: Illuminate\\Database\\Eloquent\\Collection::$id
: ->where('giveaway_id', $giveaway->id)
我的代碼:
public function raffling()
{
parent::setTitle('Items raffling | ');
$green_ticket = \DB::table('users')->where('id', $this->user->id)->value('green_ticket');
$userid = htmlspecialchars_decode($this->user->id);
$user = User::where('id', $userid)->first();
$kolvo=\DB::table('giveaway_items')->where('status',0)->orderBy('id', 'desc')->count();
$giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->take(2)->get();
$giveaway_users = \DB::table('giveaway_users')
->where('giveaway_id', $giveaway->id)
->join('users', 'giveaway_users.user_id', '=', 'users.id')
->get();
$giveAway = Giveaway::where('winner_id', '!=', 'NULL')->orderBy('created_at', 'DESC')->first();
$user = User::find($giveAway->winner_id);
$username = $user->username;
$userava = $user->avatar;
$usersteamid = $user->steamid64;
$is_enter = \DB::table('giveaway_users')->where('giveaway_id', $giveaway->id)->where('user_id', $userid)->count();
return view('pages.raffling', compact('kolvo', 'giveaway', 'giveaway_users', 'giveAway', 'user', 'username', 'userava',
'usersteamid', 'green_ticket', 'is_enter'));
}
var_dump($giveaway)
with ->first()
at variable 告訴我這個:
object(App\Giveaway)#586 (23) { ["table":protected]=> string(8) "giveaway" ["fillable":protected]=> array(5) { [0]=> string(8) "max_user" [1]=> string(5) "items" [2]=> string(9) "winner_id" [3]=> string(13) "green_tickets" [4]=> string(5) "price" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(103) ["max_user"]=> int(2) ["items"]=> string(472) "[{"id":1025,"assetid":"18251557509","market_hash_name":"Test","classid":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KW1Zwwo4NUX4oFJZEHLbXK9QlSPcU_phVWSVXvTO2j0IDeXFN_IB1ovbOrLDhp3v7HYylD4OOhkYGbmPm7PrTfnW5I1854hO7-_IH4h0agqh8DJDyiZNnLbAE8M13Q-Ae4wrq7g5Pq7cufnCRm7nZ3tCyPlhSyhx1IabZrjPKaQVqAR_se2_6rU3g","price":31.86,"steamid":"1","type":"card","bot":"1","status":0,"created_at":"2020-03-03 17:36:32","updated_at":"2020-03-14 17:41:25","is_withdraw":0,"is_raffling":0}]" ["finished_at"]=> NULL ["winner_id"]=> NULL ["status"]=> int(0) ["created_at"]=> string(19) "2020-03-18 21:08:52" ["updated_at"]=> string(19) "2020-03-18 21:08:52" ["name"]=> string(0) "" ["green_tickets"]=> int(2) ["price"]=> float(31.86) ["classid"]=> string(0) "" } ["original":protected]=> array(12) { ["id"]=> int(103) ["max_user"]=> int(2) ["items"]=> string(472) "[{"id":1025,"assetid":"18251557509","market_hash_name":"Test","classid":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KW1Zwwo4NUX4oFJZEHLbXK9QlSPcU_phVWSVXvTO2j0IDeXFN_IB1ovbOrLDhp3v7HYylD4OOhkYGbmPm7PrTfnW5I1854hO7-_IH4h0agqh8DJDyiZNnLbAE8M13Q-Ae4wrq7g5Pq7cufnCRm7nZ3tCyPlhSyhx1IabZrjPKaQVqAR_se2_6rU3g","price":31.86,"steamid":"1","type":"card","bot":"1","status":0,"created_at":"2020-03-03 17:36:32","updated_at":"2020-03-14 17:41:25","is_withdraw":0,"is_raffling":0}]" ["finished_at"]=> NULL ["winner_id"]=> NULL ["status"]=> int(0) ["created_at"]=> string(19) "2020-03-18 21:08:52" ["updated_at"]=> string(19) "2020-03-18 21:08:52" ["name"]=> string(0) "" ["green_tickets"]=> int(2) ["price"]=> float(31.86) ["classid"]=> string(0) "" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) }
我也嘗試將$giveaway->id
更改$giveaway->id
$giveaway[0]->id
,但沒有任何改變。
並且在刀片中全部顯示為@foreach(json_decode($giveaway->items) as $item)
我的問題在哪里? 我該如何解決這個錯誤?
$giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->take(2)->get();
這一行從贈品中提取了兩條記錄,
所以你需要使用pluck
,以獲得兩個id
從$giveaway
,然后用whereIn
用於過濾的結果:
$giveaway_users = \DB::table('giveaway_users')
->whereIn('giveaway_id', $giveaway->pluck('id'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.