Php laravel未定义属性:Illumb\Database\Eloquent\Collection::$id

Php laravel未定义属性:Illumb\Database\Eloquent\Collection::$id,php,laravel,Php,Laravel,该站点有一个带有绘图的页面。默认情况下,最后的绘图通过以下方式显示:$giveaway=giveaway::orderBy('id','desc')->where('status',0)->first(),一切都很好。当我尝试添加另一个绘图时,将不再显示上一个绘图,并显示最新的绘图 我试图更改$giveaway(正如我在第一行中解释的那样): $giveway=giveway::orderBy('id','desc')->where('status',0)->take(2)->get() 我收到

该站点有一个带有绘图的页面。默认情况下,最后的绘图通过以下方式显示:
$giveaway=giveaway::orderBy('id','desc')->where('status',0)->first(),一切都很好。当我尝试添加另一个绘图时,将不再显示上一个绘图,并显示最新的绘图

我试图更改
$giveaway
(正如我在第一行中解释的那样):

$giveway=giveway::orderBy('id','desc')->where('status',0)->take(2)->get()

我收到错误
PagesController.php第385行:未定义的属性:illumb\Database\elounce\Collection::$id
,385行:
->其中('giveaway\u 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\u dump($giveaway)
->first()
在变量处告诉我:

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) }
我还尝试将
$giveway->id
更改为
$giveway[0]->id
,但没有任何更改

在blade中,所有内容都由
@foreach(json_decode($giveaway->items)显示为$item)

我的问题在哪里?如何修复此错误?

$giveway=giveway::orderBy('id','desc')->where('status',0)->take(2)->get();
这行从Giveway中取出两张唱片

因此,您需要使用
pulk
$giveaway
获取两个
id
,然后使用
其中
过滤结果:

$giveaway\u users=\DB::table('giveaway\u users'))
->其中('giveway_id',$giveway->pull('id'))

这是否回答了您的问题?旁注:从字面上看,这是同一个问题,答案很可能是sameyes,我问题的这个答案,它解决了那个问题,但这个答案并不能解决这个问题