Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ctyler's avatar

Javascript and php

I am using a drag and drop application called Gridstack.js for a dashboard. It works very well. Users can drag and drop widgets and resize with handles, etc. I am saving this data in the database via json field.

In the html there is a div

 <div class="grid-stack">

            </div>

The json is stored like this:

let serializedData = [
      {x: 0, y: 0, w: 2, h: 2, id: '0'},
      {x: 3, y: 1, h: 2, id: '1', content: "<button onclick=\"alert('clicked!')\">Press me</button>"},
      {x: 4, y: 1, id: '2'},
      {x: 2, y: 3, w: 3, id: '3'},
      {x: 1, y: 3, id: '4'}
    ];

Then it is loaded into the DOM like this:

 loadGrid = function () {
                    grid.load(serializedData, true); // update things
                }

When the layout is saved it is storing a hidden field that is populated with json.

public function store(Request $request)
    {
        $dashboard = auth()->user()->dashboard;
        $dashboard->layout = json_encode($request->dashboard);
        $dashboard->save();
        return back();
    }

The dashboard is loaded like this:

$data = json_decode($user->dashboard->layout);

        if(is_null($data)) {
            $data = '[]';
        }

The problem is the content in the json. I will be wanting to show query result and stuff like that as part of the contents. What would be the best way to do that? I cant call PHP from the javascript. Any help in this would be helpful.

0 likes
6 replies
ctyler's avatar

@jlrdw Sorry I am not understanding. Are you saying, store the content in a related table? I am not really sure how that would help. So if I created a widget that would show the most recent support ticket count, How would I show that? The widgets can sometime show dynamic data. It seems like I would need to run the query in the controller, and concatenate the content to the json then pass it to the view?

Is there something I could do with a component?

ctyler's avatar

@Tray2 So this kind of pulled my out of my thought process so if you could help me. So each user has a one to one relation ship with a dashboard table. The dashboard has a layout column that contains the json. So what you are saying is, you would create a one to many to a layouts (or widgets) table with columns: dashboard_id, x,y,w,h, id, and content that would store the data on each widget?

ctyler's avatar

@Tray2 The more I think about it the more I like that and it fits better into the application. I appreciate your input.

1 like

Please or to participate in this conversation.