简体   繁体   English

如何拆分数组并仅将特定值保存到数据库中? Laravel 与 Ajax

[英]How to split array and only save specific value into database? Laravel with Ajax

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.我正在使用 Ajax 并将数据传递给控制器​​。

ps: the array set can be more than 1 so each set need to split and store based on columns inside DB. ps:数组集合可以超过1个,所以每个集合都需要根据数据库中的列进行拆分和存储。

my javascript that contain Ajax:我的包含 Ajax 的 javascript:

 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

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);

The problem appears to be parsing of data to hotspots .问题似乎是将data解析为hotspots This method is iterating over each entry of data , then assigning the full node.此方法迭代每个data条目,然后分配完整节点。

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

Each property definition is using the full hotspots object, as opposed to one property.每个属性定义都使用完整的hotspots对象,而不是一个属性。

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?例如, hotspots应该是一组hotspot对象……你怎么知道你要为单个请求发送哪个?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM