douaipi3965 2016-12-12 18:07
浏览 152
已采纳

Laravel Argument 1传递给了

I'm trying to do my coding, but I ran into this issue. Thing is, I did it exactly the same like my first code, but it's working there.

ErrorException in CoinflipController.php line 115: Argument 1 passed to App\Http\Controllers\CoinflipController::CoinflipToJson() must be an instance of App\Models\Game\Coinflip\Coinflip, instance of Illuminate\Database\Eloquent\Collection given, called in C:\xampp\htdocs\site\app\Http\Controllers\CoinflipController.php on line 104 and defined

Coinflip File

<?php

namespace App\Models\Game\Coinflip;

use Illuminate\Database\Eloquent\Model;

class Coinflip extends Model {
    const STATUS_ACTIVE     = 0;
    const STATUS_ROLLING    = 1;
    const STATUS_ENDED      = 2;

    protected $table = 'coinflip';    
    protected $fillable = [
        'status',
        'winner_steam_id',
        'winner_probability',
        'winner_value',
        'hash',
        'ticket',
        'seed',
        'player1',
        'player1_side',
    ];
    protected $dates = [ 'draw_at' ];
    protected $casts = [
        'winner_steam_id' => 'string',
        'winner_probability' => 'float',
        'winner_value' => 'float',
        'ticket' => 'double'
    ];

    public function entry(){
        return $this->hasMany( 'App\Models\Game\Coinflip\CoinflipEntry', 'coinflip_id' );
    }

    public function winner(){
        return $this->hasOne( 'App\User', 'steam_id', 'winner_steam_id' );
    }

    public function getCommissionValue(){
        $val = 0;
        foreach( $this->entry as $entry ){
            foreach( $entry->item as $item ){
                if ( $item->status == CoinflipEntryItem::STATUS_COMMISIONED )
                    $val += (float)$item->price;
            }
        }
        return $val;
    }
}

CoinflipToJson Function (From 1st line is the Error)

 public function CoinflipToJson( Coinflip $coinflip, $showExtra = false ){
        $canDeposit1 = $coinflip->value * 0.10 + $coinflip->value;
        $canDeposit2 = $coinflip->value - $coinflip->value * 0.10;
        $data = [
            'id' => $coinflip->id,
            'hash' => $coinflip->hash,
            'gameValue' => $coinflip->value,
            'canDeposit1' => $canDeposit1,
            'canDeposit2' => $canDeposit2,
            'skinValue' => 0,
            'skinCount' => 0,
            'timeStart' => $coinflip->created_at->getTimestamp(),
            'timeEnd' => $coinflip->draw_at ? $jackpot->draw_at->getTimestamp() : 0,
            'timeEndIn' => $coinflip->draw_at ? $jackpot->draw_at->getTimestamp() - time() : -1,
            'timeMax' => Setting::of('JackpotExtendTime', 15)->getValue(),
            'entry' => []
        ];

        if ( $showExtra ){
            $data['winningPercentage'] = $coinflip->ticket;
            $data['winnerId'] = $coinflip->winner_steam_id;
            $data['secret'] = $coinflip->seed;
        }

        foreach( $coinflip->entry as $entry ){
            $entryData = $this->entryToJson( $entry );
            $data['entry'][] = $entryData;
            $data['skinValue'] += $entryData['skinValue'];
            $data['skinCount'] += $entryData['skinCount'];
        }
        return $data;
    }

Code where I am calling it (Line 5)

public function current( Request $request ){
    $coinflip = $this->getCurrentGame();
    if($coinflip){
        $data = [
            'current' => $this->CoinflipToJson($coinflip)
        ];
        return response()->json($data);
    } else return response()->json(['error' => 'No Games']);
}

getCurrent Game Function

public function getCurrentGame(){
    $coinflip = Coinflip::where('status', Coinflip::STATUS_ACTIVE)->get();
    return $coinflip;
}
  • 写回答

1条回答 默认 最新

  • doulong2782 2016-12-12 19:39
    关注

    In your getCurrentGame() method, the get() method always returns a Collection, even if there was only one record. If you query will only ever return one record, you can just change get() to first(), and it will return the record instance, instead of a Collection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流