user2480176
user2480176

Reputation: 147

Save .JSON file to database using laravel

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.

Edit:

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,
  }
}}  

Upvotes: 2

Views: 8990

Answers (1)

Scopey
Scopey

Reputation: 6319

Add this line to your migration file:

$table->string('json');

Refresh your migrations

php artisan migrate:refresh

Add to your user object:

class User extends Eloquent
{
    public $json;
}

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

Save

$user->save();

Upvotes: 7

Related Questions