简体   繁体   English

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

[英]laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id

The site has a page with draws.该网站有一个抽奖页面。 By default, the last draws show via $giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->first();默认情况下,最后一次开奖通过$giveaway = Giveaway::orderBy('id', 'desc')->where('status', 0)->first(); , all was fine. ,一切都很好。 When i try to add another draw, the previous one is no longer displayed and the newest draw is displayed.当我尝试添加另一个抽奖时,不再显示前一个抽奖并显示最新的抽奖。

I try to change $giveaway (as i explain at first line) like this:我尝试像这样更改$giveaway (正如我在第一行解释的那样):

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

And i received error PagesController.php line 385: Undefined property: Illuminate\\Database\\Eloquent\\Collection::$id , 385 line it's: ->where('giveaway_id', $giveaway->id)我收到错误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)

My code:我的代码:

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 say me this: 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) }

Also i try change $giveaway->id to $giveaway[0]->id , but nothing changed.我也尝试将$giveaway->id更改$giveaway->id $giveaway[0]->id ,但没有任何改变。

And in the blade all show by @foreach(json_decode($giveaway->items) as $item)并且在刀片中全部显示为@foreach(json_decode($giveaway->items) as $item)

Where is my problem?我的问题在哪里? How i can fix this error?我该如何解决这个错误?

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

This line take two records from Giveaway,这一行从赠品中提取了两条记录,

so you need to use pluck to get two id from $giveaway , then use whereIn for filtering the result:所以你需要使用pluck ,以获得两个id$giveaway ,然后用whereIn用于过滤的结果:

$giveaway_users = \DB::table('giveaway_users')
                    ->whereIn('giveaway_id', $giveaway->pluck('id'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3 - Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3 Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection - Undefined property: Illuminate\Database\Eloquent\Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM