未定义的属性:Illuminate \ Database \ Eloquent \ Builder :: $ winner_id [英] Undefined property: Illuminate\Database\Eloquent\Builder::$winner_id

查看:44
本文介绍了未定义的属性:Illuminate \ Database \ Eloquent \ Builder :: $ winner_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个抽奖系统.现在数据库中有2个抽奖,但是由于某种原因,在抽奖页面上仅显示最后创建的抽奖.我的代码是:

I have a draw system on my site. There are 2 draws in the database now, but for some reason, only the last created draw is displayed on the draw page. My code is:

public function raffling()
{
    parent::setTitle('Items raffling | ');
    $green_ticket = \DB::table('users')->where('id', $this->user->id)->value('green_ticket');

    $kolvo=\DB::table('giveaway_items')->where('status',0)->orderBy('id', 'desc')->count();
    $giveaway = Giveaway::orderBy('id', 'desc')->first();
    $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;


    return view('pages.raffling', compact('kolvo', 'giveaway', 'giveaway_users', 'giveAway', 'user', 'username',
                                         'userava', 'usersteamid', 'green_ticket'));

}

我尝试在 $ giveaway = Giveaway :: orderBy('id','desc')-> first(); 而不是 first()进行更改>使用 take(2),但是我收到错误 Undefined property:Illuminate \ Database \ Eloquent \ Builder :: $ winner_id .如果我在行 $ giveAway = Giveaway :: where('winner_id','!=','NULL')-> orderBy中添加 take(2)('created_at','DESC')-> first(); 而不是 first().

I tried make changes at $giveaway = Giveaway::orderBy('id', 'desc')->first(); and instead of first() use take(2), but i received error Undefined property: Illuminate\Database\Eloquent\Builder::$winner_id. And the same error appears if I add take(2) to the line $giveAway = Giveaway::where('winner_id', '!=', 'NULL')->orderBy('created_at', 'DESC')->first(); instead of first().

我的问题在哪里,我该如何解决该错误?

Where is my problem, how i can fix this error?

推荐答案

使用 first()方法意味着只检索一条记录.

When you have used first() method means only one records retrieve.

现在,正如您所说的,您希望从数据库中获得两条记录.这里有两种方法.

Now as you said you want two records from the database. There are two methods available here.

1)take()

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

2)limit()

$giveaway = Giveaway::orderBy('id', 'desc')->limit(2)->get();

*发生您的问题是因为您没有使用 get()方法.

*Your problem occurs because you have not used the get() method.

这篇关于未定义的属性:Illuminate \ Database \ Eloquent \ Builder :: $ winner_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆