2

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,
  }
}}  
1
  • Please can you give us an example of the json file? Commented Jul 1, 2014 at 2:42

1 Answer 1

7

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I'm a little confused on the 'test-key' => 'test-data' part. What exactly is that doing? How could I set the example JSON in my question as default after creating the new user?
That $user->json = line is just where I'm setting the json on the object. json_encode turns a PHP array into a JSON string. Depending on what form your json is in, you will just need to assign it to the $user->json property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.