I have Laravel 5 red documentation and I saw that it supports json type fields in the database, but I noticed really strange behavior.
This is my table:
Schema::create('subjects', function ($table) { $table->increments('id'); $table->string('name', 100); $table->integer('ects'); $table->json('studies');
Then I sow the database:
Subject::create(['name' => 'Programming 1', 'ects' => '0', 'studies' => '{"program":"Computer Science","year":"1"}']);
The server response looks something like this:
{ "id": 15, "name": "Programming 1", "brEcts": 0, "studies": "{\"program\":\"Computer Science\",\"year\":\"1\"}", "created_at": "2015-12-05 00:00:00", "updated_at": "2015-12-05 00:00:00", "pivot": { "profesor_id": 20, "subject_id": 15 } }
Pay attention to βin the field studies of answersβ, and the βrotationβ field that laravel generates for me is correctly structured and also a field of type json.
When I looked at phpMyAdmin, the value of the research field looks fine. (without\")
My server code for the response:
$subjects = Profesor::find($id)->subjets()->get(); return response()->json($subjects);
Am I correct database seed, or is the problem when I return the value on the server?
I know that I can solve this problem on the client side by removing the "\" characters, but this is my last option as it is not clean enough.
EDIT:
I solved this by adding an array to the Model class:
protected $casts = [ 'name' => 'string', 'ects' => 'integer', 'studies' => 'array' ];
The documentation can be seen here.