Laravel JSON fields and set and get attribute

Created At: 2023-06-13 01:21:32 Updated At: 2023-06-13 01:24:58

JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn't we? This tutorial will show you an example.

In our application, we have stored JSON data in the JSON field, so we need to know how to save data in the field and get data back from the field.

Here's our Lesson model


namespace App\Models;

use Encore\Admin\Traits\DefaultDatetimeFormat;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
    use HasFactory;
    use DefaultDatetimeFormat;

    protected $casts = [

    public function setVideoAttribute($value){

        $newVideo = [];
        foreach($value as $k=>$v){
            $valueVideo = [];
                $valueVideo['url'] = $v['old_url'];

            if (!empty($v["old_thumbnail"])) {
                $valueVideo["thumbnail"] = $v["old_thumbnail"];
            } else {
                $valueVideo["thumbnail"] = $v["thumbnail"];

        //json_encode makes it json for the database
        //array_values get the values of the php associative array
        $this->attributes['video'] = json_encode(array_values($newVideo));

    public function getVideoAttribute($value){
        //conver to associative array
        $result = json_decode($value, true);
            foreach($result as $key => $value){
                $result[$key]['url'] = env('APP_URL')."uploads/".$value['url'];
                $result[$key]['thumbnail'] = env('APP_URL') . "uploads/" . $value['thumbnail'];

        return $result;

    public function getThumbnailAttribute($value){
        return env('APP_URL')."uploads/".$value;


Here's the controller that get data from the front end form and data process. It happens in the form() method.

    protected function form()
        $form = new Form(new Lesson());

        //$form->display('id', __('ID'));
        $result = Course::pluck('name', 'id');
        $form->text('name', __('Name'));
        $form->select('course_id', __('Courses'))->options($result);
        $form->image('thumbnail', __('Thumbnail'))->uniqueName();
        $form->textarea('description', __('Description'));


            //access this during form eddting
            // dump($form->video);

                $form->table('video', function ($form) {

                //any kind of media


                //normal form submission or form creating

                $form->table('video', function ($form) {
                //any kind of media
        //saving call back gets called before submitting to the datbase
        //but after clicking the submit button
        // a good place to process grabbed data or form data
        $form->saving(function (Form $form){
                //here is the place to process data and
                //the below one gets the editted data
                $video = $form->video;
                // the below gets data from the database
                $res = $form->model()->video;
                //for each of the key, get the value
                $path = env('APP_URL') . "uploads/";

                $newVideo = [];
                foreach ($video as $k => $v) {
                    $oldVdieo = [];
                    //user did not type anything
                    if (empty($v['url'])) {
                        $oldVdieo["old_url"] = empty($res[$k]['url']) ? ""
                            //replacing the domian path from the value
                            : str_replace($path, "", $res[$k]['url']);
                    } else {
                        //this is a new editted value
                        $oldVdieo["url"] = $v['url'];

                    if (empty($v['thumbnail'])) {
                        $oldVdieo["old_thumbnail"] = empty($res[$k]['thumbnail']) ? ""
                            //replacing the domian path from the value
                            : str_replace($path, "", $res[$k]['thumbnail']);
                    } else {
                        //this is a new editted value
                        $oldVdieo["thumbnail"] = $v['thumbnail'];

                    if (empty($v['name'])) {
                        $oldVdieo["name"] = empty($res[$k]['name']) ? ""

                            :  $res[$k]['name'];
                    } else {
                        //this is a new editted value
                        $oldVdieo["name"] = $v['name'];

                    array_push($newVideo, $oldVdieo);
                   // dump($newVideo);
                $form->video = $newVideo;


        return $form;


Add Reviews