1

I'm using laravel with eloquent and a mysql database.

There is a JSON field in my database:

class CreateJogoDetalhesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tableX', function (Blueprint $table) {
            $table->increments('id');
            [... others ...]
            $table->json('numbers');
    }
[...]

When I retrieve the data on my model/api route:

Route::middleware('cors:api')->get('/MYROUTE', function (Request $request) {
    $resource= Tablex::with('tb1','tb2','tb3')->get();
    return $resource->toJson();
});

My mysql json field comes with a string format:

tableX": {
      "id": 1,
      "name": "foo",
      "values": "{\"6\": 3.5, \"7\": 24.5, \"8\": 24.5, \"9\": 24.5, \"10\": 24.5, \"11\": 24.5, \"12\": 24.5, \"13\": 24.5, \"14\": 24.5, \"15\": 24.5}",
    },

But I need them on this format:

"tableX": {
      "id": 1,
      "name": "foo",
      "values": {
        "6": 3.5,
        "7": 24.5,
        "8": 24.5,
        "9": 24.5,
        "10": 24.5,
        "11": 24.5,
        "12": 24.5,
        "13": 24.5,
        "14": 24.5,
        "15": 24.5
      },

How can I ask to laravel catch the data on this format?

7
  • Read the manual for json_decode() Commented Nov 27, 2018 at 19:08
  • Although you may have stored that data incorrectly as it is not a valid json string Commented Nov 27, 2018 at 19:10
  • I know the json_decode function. But this is a pure php solution. I want a solution that use laravel/eloquent to solve the string json problem. Commented Nov 27, 2018 at 19:33
  • json_decode() is PURE PHP Commented Nov 27, 2018 at 19:34
  • It is a valid json. The json type in mysql do all json validation before accept the data. Commented Nov 27, 2018 at 19:35

1 Answer 1

7

The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model:

class User extends Model
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'values' => 'array',
    ];
}

https://laravel.com/docs/5.7/eloquent-mutators#array-and-json-casting

This will convert it to an array on the PHP side and will properly include the JSON data when Laravel serializes the model.

Sign up to request clarification or add additional context in comments.

1 Comment

Ohh man! Array & JSON Casting that is exactly what i was looking for. Thank you!

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.