使用laravel将.JSON文件保存到数据库 [英] Save .JSON file to database using laravel

查看:778
本文介绍了使用laravel将.JSON文件保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常对Laravel和PHP还是陌生的,我从事的大多数工作都是与在线教程有关的.我知道如何将用户名或密码之类的单个项目保存到数据库中,但是在存储整个文件时我一无所知.这是我的数据库当前在迁移文件中的格式:

I am very new to Laravel and PHP in general, most of what I have worked on has been relative to online tutorials. I know how to save single items like a username or password to my database but I am clueless when it comes to storing an entire file.This is how my database is currently formatted in my migration file:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('remember_token')->nullable();
        $table->timestamps();
    });
}

感谢您的帮助,请告知我需要有关我的项目的更多信息.

Any help is appreciated, let me know is more information about my project is needed.

类似于我将要使用的JSON文件的示例:

Example of JSON file similar to what I will be using:

{"Date 1": {
  "Item 1": {
    "para1": "word",
    "para2": 100,
  },
  "Item 2": { 
    "para1": "word",
    "para2": 100,
  },
  "Item 3": {
    "para1": "word",
    "para2": 100,
  }
  }
"Date 2": {
  "Item 1": {
    "para1": "word",
    "para2": 100,
  },
  "Item 2": { 
    "para1": "word",
    "para2": 100,
  },
  "Item 3": {
    "para1": "word",
    "para2": 100,
  }
}}  

推荐答案

将此行添加到您的迁移文件中:

Add this line to your migration file:

$table->string('json');

刷新您的迁移

php artisan migrate:refresh

添加到您的用户对象:

class User extends Eloquent
{
    public $json;
}

照常设置属性(建议使用setter,但在此示例中未提供)

Set the property as usual (a setter is recommended but not provided in this example)

$user = new User;
$user->json = json_encode( array('test-key' => 'test-data' ) );

保存

$user->save();

这篇关于使用laravel将.JSON文件保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆