简体   繁体   中英

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(); , 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::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)

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:

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.

And in the blade all show by @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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM