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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog