【问题标题】:Laravel Argument 1 passed toLaravel 参数 1 传递给
【发布时间】:2016-12-13 03:39:19
【问题描述】:

我正在尝试编写代码,但遇到了这个问题。问题是,我做的和我的第一个代码完全一样,但它在那里工作。

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 上 第 104 行并定义

硬币翻转文件

<?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 函数(第一行是错误)

 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']);
}

获取当前游戏函数

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

【问题讨论】:

标签: php laravel


【解决方案1】:

在您的getCurrentGame() 方法中,get() 方法总是返回一个Collection,即使只有一条记录。如果您查询只会返回一条记录,您只需将get() 更改为first(),它将返回记录实例,而不是Collection

【讨论】:

  • 小问题,如果我不能使用 get() 那么如何从表中获取所有值?
  • @AndremarGrim get() 将返回表中的所有值。您当前代码的问题是 CoinflipToJson() 需要一个 CoinFlip 实例,而您正试图传递一个 Collection 的硬币翻转,因为这是从您的 getCurrentGame() 方法返回的内容。
  • 嗯,但我必须改变它以使用 Collection?
  • @AndremarGrim 这一切都取决于您的要求。我将假设您的 json 实际上应该能够返回当前游戏的数组。在这种情况下,不要管你的getCurrentGame()(保持为get()),然后将'current' =&gt; $this-&gt;CoinflipToJson($coinflip) 更改为'current' =&gt; $coinflip-&gt;map(function ($item) { return $this-&gt;CoinflipToJson($item); })-&gt;all()。这将遍历您的 $coinflip 集合中的项目,并使用您的 CoinflipToJson() 方法对其进行转换。
  • 你能编辑你的第一篇文章吗,我太笨了,我无法“修复”[]的问题。
猜你喜欢
  • 2020-03-24
  • 1970-01-01
  • 2023-03-10
  • 2017-12-23
  • 1970-01-01
  • 2015-10-09
  • 2020-01-06
  • 2019-01-01
  • 2015-01-04
相关资源
最近更新 更多