0

I have a question. I need to save array into database but I need to divide it first and only save specific value. I am using Ajax and pass the data to controller.

ps: the array set can be more than 1 so each set need to split and store based on columns inside DB.

my javascript that contain Ajax:

Hotspot.prototype.saveData = function (data) {
        if (!data.length) {
            return;
        }

        // Get previous data
        var raw_data = localStorage.getItem(this.config.LS_Variable);

        var hotspots = [];

        if (raw_data) {
            hotspots = JSON.parse(raw_data);
        }
    
        // Append to previous data
        $.each(data, function (index, node) {
            hotspots.push(node);
        });
        console.log(hotspots);

        // var field=JSON.stringify(hotspots).split(',');
        this.data=data;
        $.ajax({
            type:"POST",
            url:"/store",
            dataType:'json',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data:{
                Title: JSON.stringify(hotspots),
                Message: JSON.stringify(hotspots),
                x: JSON.stringify(hotspots), 
                y: JSON.stringify(hotspots),
            },
            success: function(data){

                console.log(data,d);
            },
            error: function(data)
            {
                console.log(data);
            },
            
        });

        localStorage.setItem(this.config.LS_Variable, JSON.stringify(hotspots));

        this.element.trigger('afterSave.hotspot', [null, hotspots]);
    };

Controller:

public function storePin(Request $request)
    {
       request()->validate([
           'Title' => 'required',
            'Message'  => 'required',
            'x'=> 'required',
            'y'=>'required',
       ]);

           dd($request);
             if ($request->all())
             {
                 $pin = new Pin();
                 $pin->Title=json_encode($request->input('Title'));
                 $pin->Message= json_encode($request->input('Message'));
                 $pin->x = json_encode($request->input('x'));
                 $pin->y =json_encode($request->input('y'));

                 $pin->save();
                //  return response()->json_encode($request);

             }

    }

example output:

Title: [{"x":58.333333333333336,"y":90.54545454545455,"Title":"hi","Message":"hi"}]
Message: [{"x":58.333333333333336,"y":90.54545454545455,"Title":"hi","Message":"hi"}]
x: [{"x":58.333333333333336,"y":90.54545454545455,"Title":"hi","Message":"hi"}]
y: [{"x":58.333333333333336,"y":90.54545454545455,"Title":"hi","Message":"hi"}]

based on this I only want that it only store:

Title:only save title
Message:save message
x:save x
y save y
1
  • if u guys want to grasp the basic of my project click this link and try the demo jqueryscript.net/demo/… Commented Feb 21, 2020 at 14:24

2 Answers 2

1

Just pass in the whole array of hotspots like:

data: hotspots,

Then in your model do any formatting and insert many:

// some formatting to create data array
$data = [];
foreach($hotspots as $hotspot){
    $data[] = [
        'Title' => $hotspot['Title'],
        'Message'  => $hotspot['Message'],
        'x' => $hotspot['x'],
        'y' => $hotspot['y'],
    ];
}

Pin::insert($data);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer seems to make the most sense.
0

The problem appears to be parsing of data to hotspots. This method is iterating over each entry of data, then assigning the full node.

$.each(data, function(index, node) {
  hotspots.push(node);
});

Each property definition is using the full hotspots object, as opposed to one property.

data: {
  Title: JSON.stringify(hotspots),
  Message: JSON.stringify(hotspots),
  x: JSON.stringify(hotspots),
  y: JSON.stringify(hotspots),
},

You probably need to do something like this:

{
  Title: hotspots[0].Title,
  Message: hotspots[0].Message,
  x: hotspots[0].x,
  y: hotspots[0].y,
}

Even still, this solution is missing some important information. For example, hotspots should be an array of hotspot objects...how do you know which one you are going to send for the single request?

1 Comment

i already tried it work...but its only take 1 set of array......so the other set is not saved like here i have 3 set of array 0: {x: 44.135802469135804, y: 93.63636363636364, Title: "1", Message: "1"} 1: {x: 32.03703703703704, y: 87.45454545454545, Title: "2", Message: "2"} 2: {x: 67.5925925925926, y: 82.9090909090909, Title: "3", Message: "3"} but its only save the first array

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.