Php Laravel参数1传递给
我正在尝试编写代码,但遇到了这个问题。问题是,我做的和我的第一个代码完全一样,但它在那里工作 CoinflipController.php第115行中的ErrorException:传递了参数1 要应用\Http\Controllers\CoinflipController::CoinflipToJson(),必须 App\Models\Game\Coinflip\Coinflip的实例 Illumb\Database\Elount\Collection已给定,已调用 上的C:\xampp\htdocs\site\app\Http\Controllers\CoinflipController.php 第104行已定义 Coinflip文件Php Laravel参数1传递给,php,laravel,Php,Laravel,我正在尝试编写代码,但遇到了这个问题。问题是,我做的和我的第一个代码完全一样,但它在那里工作 CoinflipController.php第115行中的ErrorException:传递了参数1 要应用\Http\Controllers\CoinflipController::CoinflipToJson(),必须 App\Models\Game\Coinflip\Coinflip的实例 Illumb\Database\Elount\Collection已给定,已调用 上的C:\xampp\ht
<?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;
}
}
我调用它的代码(第5行)
获取当前游戏函数
public function getCurrentGame(){
$coinflip = Coinflip::where('status', Coinflip::STATUS_ACTIVE)->get();
return $coinflip;
}
在
getCurrentGame()
方法中,即使只有一条记录,get()
方法始终返回一个集合。如果查询只返回一条记录,只需将get()
更改为first()
,它将返回记录实例,在您的问题中添加一点示例代码,而不是一个集合
,这将使除猜测之外的任何东西都更易于使用。请阅读并了解如何创建Hello,对此我深表歉意。我将添加pastebin链接。Nooooooo。在这里粘贴代码而不是代码链接我已经更新了我的第一篇文章。我不能在这里添加那么多代码。我必须键入关于我帖子的“更多信息”::小问题,如果我不能使用get(),那么我如何才能从表中获取所有值?@AndremarGrimget()
将返回表中的所有值。当前代码的问题是,CoinflipToJson()
需要一个CoinFlip
实例,而您正试图传入一个硬币投掷集合,因为这是从getCurrentGame()
方法返回的。嗯,“但是我需要做什么来改变它以配合收藏?”安德烈·马格里姆(AndremarGrim)这取决于你的需求。我假设您的json实际上应该能够返回当前游戏的数组。在这种情况下,将您的getCurrentGame()
单独放置(保持为get()
),然后将'current'=>$this->CoinflipToJson($coinflip)
更改为类似'current'=>$coinflip->map(函数($item){return this->CoinflipToJson($item);})->all()
。这将循环遍历您的$coinflip
集合中的项目,并使用您的CoinflipToJson()
方法转换它们。您能编辑您的第一篇帖子吗?我太笨了,无法“修复”[]的问题。
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']);
}
public function getCurrentGame(){
$coinflip = Coinflip::where('status', Coinflip::STATUS_ACTIVE)->get();
return $coinflip;
}