将多个记录从json文件导入到Laravel项目内的DB中

时间:2015-09-13 10:58:46

标签: json laravel import

解决!将在今晚发布工作答案。可能对别人有帮助。

我正在尝试运行几个月前制作的脚本。该脚本设置为与填充.json文件的目录(每个包含200个项目的45个文件)一起使用,并运行这些文件以获取数据,然后将其与查询一起存储在数据库中。

问题是我不能在我的laravel项目中使用$ conn所以我不知道如何我现在可以连接到数据库并存储我的所有数据。我可以读取json没问题,我在laravel项目中有一个模型用于相应的表,所有字段都设置为可填充。

这是我的代码:

 <?php
namespace App\Http\Controllers;

use DB;
use App\Http\Controllers\Controller;

class JsonController extends Controller
{
    public function importJson() {
$path = realpath('AllSetFiles/');

        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
        {
            if ($filename->isDir()) continue;

            // Include the JSON and put it inside $data.
            $jsondata = file_get_contents($filename);
            $data = json_decode($jsondata, true);

            // Echo the variables to the screen or put them in the database. (y shows to screen)
            $echo = "n";

            // foreach card in cards in the JSON file put all values in their named variables.
            foreach($data['cards'] as $cards => $values) {

                if(isset($values['multiverseid'])) {
                    $multiverseid = $values['multiverseid'];
                } else {
                    $multiverseid = "";
                }


                $name = $values['name'];


                if (isset($values['manaCost'])) {
                    $manaCost_array = array();
                    $manaCost_array = array($values['manaCost']);
                    $manaCost = implode (", ", $manaCost_array);
                } else {
                    $manaCost = "";
                }


                if (isset($values['cmc'])) {
                    $cmc = $values['cmc'];
                } else {
                    $cmc = "";
                }


                if (isset($values['colors'])) {
                    $colors_array = array();
                    $colors_array = $values['colors'];
                    $colors = implode (", ", $colors_array);
                } else {
                    $colors = "";
                }


                if (isset($values['type'])) {
                    $type = $values['type'];
                } else {
                    $type = "";
                }


                if (isset($values['supertypes'])){
                    $supertypes_array = array();
                    $supertypes_array = $values['supertypes'];
                    $supertypes = implode (", ", $supertypes_array);
                } else {
                    $supertypes = "";
                }


                if (isset($values['types'])){
                    $types_array = array();
                    $types_array = $values['types'];
                    $types = implode (", ", $types_array);
                } else {
                    $types = "";
                }


                if (isset($values['subtypes'])){
                    $subtypes_array = array();
                    $subtypes_array = $values['subtypes'];
                    $subtypes = implode (", ", $subtypes_array);
                } else {
                    $subtypes = "";
                }


                if (isset($values['rarity'])) {
                    $rarity = $values['rarity'];
                } else {
                    $rarity = "";
                }


                if (isset($data['code'])) {
                    $serie = $data['code'];
                } else {
                    $serie = "";
                }


                if (isset($values['text'])) {
                    $text = $values['text'];
                } else {
                    $text = "";
                }


                if (isset($values['flavor'])) {
                    $flavor = $values['flavor'];
                } else {
                    $flavor = "";
                }


                if (isset($values['artist'])) {
                    $artist = $values['artist'];
                } else {
                    $artist = "";
                }



                if (isset($values['number'])) {
                    $number = $values['number'];
                } else {
                    $number = "";
                }


                if (isset($values['power'])) {
                    $power = $values['power'];
                } else {
                    $power = "";
                }


                if (isset($values['toughness'])) {
                    $toughness = $values['toughness'];
                } else {
                    $toughness = "";
                }


                if (isset($values['layout'])) {
                    $layout = $values['layout'];
                } else {
                    $layout = "";
                }


                if (isset($values['imageName'])) {
                    $imageName = $values['imageName'];
                } else {
                    $imageName = "";
                }


                if ($echo == "y"){
                    echo 'multiverseid: <b>' . $multiverseid . '</b></br>';
                    echo 'name: <b>' . $name . '</b><br/>';
                    echo 'serie: ' . $serie . '<br/>';
                    echo 'manaCost:' . $manaCost . '<br/>';
                    echo 'cmc: ' . $cmc . '<br/>';
                    echo 'colors: ' . $colors . '<br/>';
                    echo 'type: ' . $type . '<br/>';
                    echo 'supertypes:' . $supertypes . '<br/>';
                    echo 'types: ' . $types . '<br/>';
                    echo 'subtypes: ' . $subtypes . '<br/>';
                    echo 'rarity: ' . $rarity . '<br/>';
                    echo 'text: ' . $text . '<br/>';
                    echo 'flavor: ' . $flavor . '<br/>';
                    echo 'artist: ' . $artist . '<br/>';
                    echo 'number: ' . $number . '<br/>';
                    echo 'power: ' . $power . '<br/>';
                    echo 'toughness: ' . $toughness . '<br/>';
                    echo 'layout: ' . $layout . '<br/>';
                    echo 'imageName: ' . $imageName . '<br/>';
                    echo 'src="http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=' . $multiverseid . '&type=card"<br/>';
                    echo '<br/><hr><br>';
                } else {

                    // Put all variables in a query and send it to the database.
                    DB::table('cards')->insert(
                        [
                            'multiverseid' => $multiverseid,
                            'name' => $name,
                            'serie' => $serie,
                            'manaCost' => $manaCost,
                            'cmc' => $cmc,
                            'colors' => $colors,
                            'type' => $type,
                            'supertypes' => $supertypes,
                            'types' => $types,
                            'subtypes' => $subtypes,
                            'rarity' => $rarity,
                            'text' => $text,
                            'flavor' => $flavor,
                            'artist' => $artist,
                            'number' => $number,
                            'power' => $power,
                            'toughness' => $toughness,


          'layout' => $layout,
                                'imageName' => $imageName
                            ]
                        );

                    } // end if show or query loop
                } // end foreach loop
            } // end foreach recursiveIterator
} // end of function importJson
} // end of class

?>

在我的routes.php中:

// JSON
Route::get('importjson', 'JsonController@importJson');

转到myapp.com/importjson会出现以下错误:

Class 'App\Http\Controllers\RecursiveIteratorIterator' not found

0 个答案:

没有答案