ErrorException - 数组到字符串转换[Laravel]

时间:2017-10-02 16:39:59

标签: arrays string laravel request

出于某种原因,我在通过控制器处理请求时获得了数组到字符串转换。

我的脚本在数据库中创建了多个条目,我似乎无法弄清楚我的错误在哪里。

public function storeMappool(MappoolFormRequest $request)
{
    $this->validate($request, [
        'tournament_id',
        'game_id',
        'name',
        'content',
    ]);

    $inputForm = $request->all();

    if (!TournamentMappool::where('name', $inputForm['name'])->where('tournament_id', $inputForm['tournament_id'])->exists())
    {
        print("Creating new TournamentMappool entry.<br>");
        $inputM = array();
        $inputM['tournament_id'] = $inputForm['tournament_id'];
        $inputM['name'] = $inputForm['name'];
        $mappool = TournamentMappool::create($inputM);
    }
    else
    {
        return "Mappool Name already exists.";
    }


    $maps_arr = explode("\r\n", $inputForm['content']);
    foreach ($maps_arr as $map) {
        $map_arr = explode("||", $map);
        $map_bmid = str_replace("https://osu.ppy.sh/b/", "", $map_arr[0]);

        $bmi = json_decode ( file_get_contents ( 'http://osu.ppy.sh/api/get_beatmaps?k=<api_key>&b='.$map_bmid), true )[0];

        if (!GameMap::where('external_id', $bmi['beatmapset_id'])->exists())
        {
            print("Creating new GameMap entry.<br>");
            $input = array();
            $input['game_id'] = $request->input('game_id');
            $input['external_id'] = $bmi['beatmapset_id'];
            $input['mapname'] = $bmi['artist'] . " - " . $bmi['title'];
            $gameMap = GameMap::create($input);

            $bmsi = json_decode ( file_get_contents ( 'http://osu.ppy.sh/api/get_beatmaps?k=<api_key>&s='.$bmi['beatmapset_id']), true );
            foreach ($bmsi as $mapset) {
                print("Creating new GameMapVersion entry.<br>");
                $inputV = array();
                $inputV['map_id'] = $gameMap->id;
                $inputV['version'] = $mapset['version'];
                $inputV['external_id'] = $mapset['beatmap_id'];
                $inputV['external_data'] = $mapset;
                $gameMapVersion = GameMapVersion::create($inputV);

                if ($gameMapVersion->external_id == $bmi['beatmap_id'])
                {
                    $inputMm = array();
                    $inputMm['mappool_id'] = $mappool->id;
                    $inputMm['version_id'] = $gameMapVersion->id;
                    $inputMm['modifier'] = $map_arr[1];
                    TournamentMappoolMap::create($inputMm);
                }
            }
        }
        else
        {
            $gameMap = GameMap::where('external_id', $bmi['beatmapset_id'])
                              ->where('game_id', $inputForm['game_id'])
                              ->first();
            $gameMapVersion = GameMapVersion::where('map_id', $gameMap->id)
                                            ->where('external_id', $bmi['beatmap_id'])
                                            ->first();
            $inputMm = array();
            $inputMm['mappool_id'] = $mappool->id;
            $inputMm['version_id'] = $gameMapVersion->id;
            $inputMm['modifier'] = $map_arr[1];
            TournamentMappoolMap::create($inputMm);
        }
    }
}

我希望有人能够帮助我弄清楚我的错误在哪里以及如何解决这个问题。

API返回的结果如下:

[{"beatmapset_id":"210346","beatmap_id":"495633","approved":"1","total_length":"244","hit_length":"232","version":"captin's Legend","file_md5":"afc04f1f8dcd0dfac51bae4459f052e1","diff_size":"4.3","diff_overall":"9","diff_approach":"9.3","diff_drain":"7","mode":"0","approved_date":"2015-07-03 00:20:14","last_update":"2015-06-26 00:01:19","artist":"Nanahoshi Kangengakudan","title":"IMAGINARY LIKE THE JUSTICE","creator":"pkk","bpm":"190","source":"","tags":"pkmnyab kibbleru frostings captin1 vocaloid megpoid gumi vocarock orchestra illicit sexual relations monaka \u30e2\u30ca\u30ab exit tunes presents storytellers rpg fujuniseikouyuup \u4e0d\u7d14\u7570\u6027\u4ea4\u904ap iwami takashi \u5ca9\u898b\u9678","genre_id":"7","language_id":"3","favourite_count":"887","playcount":"570073","passcount":"26473","max_combo":"1697","difficultyrating":"6.111269474029541"}]

希望有足够的信息帮助我找出错误。

如果需要任何其他类型的模型,请随时询问。

1 个答案:

答案 0 :(得分:0)

我发现错误...我已经忽略了JSON_decode部分,因为我解码它以在$ input代中使用它。

我已使用:$inputV['external_data'] = json_encode($mapset);

修复了它