Database stored JSON not casting to PHP array

Posted 11 months ago by Kryptonit3

Laravel version: 5.6.18

Database Scema

        Schema::create('twilio_users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('created_by')->unsigned();
            $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $table->integer('updated_by')->unsigned()->nullable();
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
            $table->string('name');
            $table->string('number');
            $table->string('extension')->nullable();
            $table->json('availability');
            $table->boolean('calls_enabled')->default(true);
            $table->boolean('dial_by_extension_enabled')->default(true)->nullable();
            $table->timestamps();
        });

TwilioUser Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TwilioUser extends Model
{
    protected $casts = [
        'availability' => 'array',
    ];
}

TwilioUserTableSeeder.php

<?php

use Illuminate\Database\Seeder;

use App\User;
use App\TwilioUser;

class TwilioUserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $created = User::first();
        $json = File::get('database/seeds/data/twilio_user_table_seed.json');
        $user = new TwilioUser();
        $user->created_by = $created->id;
        $user->name = 'User Name';
        $user->number = '+15025551234';
        $user->extension = '701';
        $user->calls_enabled = true;
        $user->dial_by_extension_enabled = true;
        $user->availability = $json;
        $user->save();
    }
}

\database\seeds\data\twilio_user_table_seed.json

{"sunday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"monday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"tuesday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"wednesday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"thursday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"friday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"saturday":{"enabled":true,"start":"09:00:00","end":"17:00:00"}}

php artisan tinker output depicting issue

>>> $user = TwilioUser::first();
=> App\TwilioUser {#2384
     id: 1,
     created_by: 1,
     updated_by: null,
     name: "User Name",
     number: "+15025551234",
     extension: "701",
     availability: ""{\"sunday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"monday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"tuesday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"wednesday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"thursday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"friday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"},\"saturday\":{\"enabled\":true,\"start\":\"09:00:00\",\"end\":\"17:00:00\"}}"",
     calls_enabled: 1,
     dial_by_extension_enabled: 1,
     created_at: "2018-04-30 16:40:39",
     updated_at: "2018-04-30 16:40:39",
   }
>>> $availability = $user->availability;
=> "{"sunday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"monday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"tuesday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"wednesday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"thursday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"friday":{"enabled":true,"start":"09:00:00","end":"17:00:00"},"saturday":{"enabled":true,"start":"09:00:00","end":"17:00:00"}}"
>>> $availability->sunday
PHP Notice:  Trying to get property 'sunday' of non-object in Psy Shell code on line 1
>>> $availability['sunday']
PHP Warning:  Illegal string offset 'sunday' in Psy Shell code on line 1
>>>

Am I missing something? I may not be understanding the whole "Casting" thing. Was really hoping to be able to use the auto translate between the database and the code instead of having to perform encode and decodes.

Thanks in advance.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.