【问题标题】:Input JSON type data to Laravel 5.2向 Laravel 5.2 输入 JSON 类型数据
【发布时间】:2016-05-30 16:31:05
【问题描述】:

我有这个值的文件 txt (data.txt):

{"id":"1","title":"first Testing","completed":"Yes"}

{"id":"2","title":"Second Testing","completed":"no"}

{"id":"3","title":"Third Testing","completed":"no"}

以及带有字段idtitlecompleted..的表数据。

如何使用 laravel 5.2 将 data.txt 中的所有值输入到我的表(数据)中??

之前谢谢..对不起我的英语不好..

【问题讨论】:

    标签: json laravel-5.2


    【解决方案1】:

    读取文件并将其转换为PHP数组

     $arrays = json_decode(file_get_contents('data.txt'), true);
    

    然后用简单的foreach

    foreach($arrays as $array) {
       $myObject = new Model();
       $myObject->id = $array['id'];
       $myObject->title = $array['title'];
       $myObject->completed = $array['completed'];
       $myObject->save();
    }
    

    如果你定义了$fillable属性,你也可以直接做到这一点

    foreach($arrays as $array)
       Model::create($array);
    

    Eloquent: Mass Assignment

    使用查询生成器

    DB::table('data')->insert($arrays);
    

    Query Builder: Insert

    【讨论】:

      【解决方案2】:

      试试这些代码

      在 laravel 中你可以使用文件系统来获取文件。但这是基本版本。希望这会有所帮助。

      $file = fopen("data.txt","r");
      
      while(! feof($file))
        {
          $data = json_decode(fgets($file), true);
      
          //write your code to Store your data
      
        }
      
      fclose($file);
      

      【讨论】:

        【解决方案3】:

        看到data.txt 示例,该值似乎不是格式正确的 JSON。要首先处理这种值,您需要使用\n 作为分隔符explode 您的数据。然后过滤它以消除所有空元素,然后使用json_decode映射每个项目,这应该可以工作:

        $data = file_get_contents('path/to/data.txt');
        $data = array_filter(explode("\n", $data));
        $data = array_map(function ($item) {
            return json_decode($item, true);
        }, $data);
        
        // Insert to your database table
        DB::table('mytable')->insert($data);
        

        如果您data.txt 包含格式良好的 JSON,那么您可以跳过爆炸、过滤和映射步骤:

        $data = file_get_contents('path/to/data.txt');
        
        // Insert to your database table
        DB::table('mytable')->insert($data);
        

        如果你要多做一次,建议使用 eloquent 模型updateOrCreate() 方法来防止重复行。这是一个例子:

        // Uses $data value from the first example
        foreach($data as $row) {
            MyModel::updateOrCreate(['id' => $row['id'], $row);
        }
        

        如果你想使用 Laravel 的方式(文件系统),只是一个补充。将您的data.txt 放入storage/app 文件夹,然后使用Storage 外观检索数据。

        $data = Storage::get('data.txt');
        $data = array_filter(explode("\n", $data));
        $data = array_map(function ($item) {
            return json_decode($item, true);
        }, $data);
        
        DB::table('mytable')->insert($data);
        
        // Or
        
        foreach($data as $row) {
            MyModel::updateOrCreate(['id' => $row['id'], $row);
        }
        

        【讨论】:

          猜你喜欢
          • 2019-03-08
          • 2016-06-12
          • 2016-07-09
          • 1970-01-01
          • 2016-08-23
          • 2016-11-02
          • 2016-07-09
          • 2016-05-22
          • 1970-01-01
          相关资源
          最近更新 更多