我正在尝试编写代码,但我遇到了这个问题。事情是,我和我的第一个代码完全一样,但它在那里工作。
CoinflipController.php第115行中的ErrorException:参数1已通过 到App \ Http \ Controllers \ CoinflipController :: CoinflipToJson()必须是 App \ Models \ Game \ Coinflip \ Coinflip的实例,实例 给出了Illuminate \ Database \ Eloquent \ Collection,调用 C:\ xampp \ htdocs \ site \ app \ Http \ Controllers \ CoinflipController.php on 第104行并定义
Coinflip文件
<?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功能(从第1行开始是错误)
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;
}
我所称的代码(第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游戏功能
public function getCurrentGame(){
$coinflip = Coinflip::where('status', Coinflip::STATUS_ACTIVE)->get();
return $coinflip;
}
答案 0 :(得分:0)
在getCurrentGame()
方法中,get()
方法始终返回Collection
,即使只有一条记录。如果您的查询只返回一条记录,则只需将get()
更改为first()
,它就会返回记录实例,而不是Collection
。