【问题标题】:Array is saved to database as string, now need to post string into separate input fields from database - Laravel数组作为字符串保存到数据库中,现在需要将字符串从数据库中发布到单独的输入字段中 - Laravel
【发布时间】:2018-10-07 06:47:26
【问题描述】:

所以这是一个相当复杂的问题。继我的最后一个问题之后,该问题显示了我如何将数组保存到数据库中。 "Array to string conversion" errorr Laravel - Trying to save array to database - 我可能需要更改它保存到数据库的方式才能正确显示。

我在 laravel 中使用 google maps api,用户可以在其中创建路线,将其保存(到数据库),当他们登录时可以再次查看并编辑或删除。

航点功能有效,因此单击 + 按钮会出现更多输入字段 - 这就是用户添加它们的方式。

showing.blade 中的航点函数

            var counter = 1;
            var limit = 10;
            var i = 0;
            function addInput(divName) {
              if (counter == limit) {
                alert("You have reached the limit of adding " + counter + " additional destinations");
              } else {
                var newbr = document.createElement('br');
                var newinput = document.createElement("input");
                newinput.setAttribute("name","waypoints");
                newinput.setAttribute("autocompute","on");
                newinput.setAttribute("type", "text");
                newinput.setAttribute("class", "form-control");

                // newin = (counter + 1) + "<input type=text name=waypoints autocomplete=on>";
                document.getElementById(divName).appendChild(newbr);
                document.getElementById(divName).appendChild(newinput);
                counter++;
                i++;
                console.log("cntr=" + counter + " i=" + i + " waypoints.length=" +document.getElementsByName("waypoints"));
                // var inputw = waypoints[i];
                var autocompletew = new google.maps.places.Autocomplete(newinput);
                autocompletew.setComponentRestrictions({'country':['uk','irl']});

              }
            }

它保存到数据库中,如 myroutescontroller.php 所示

public function store(Request $request)
{   
    if (Auth::check()) {

        Myroutes::create([ //posting to acc table
            'user_id' => Auth::user()->id,
            'start' => $request->start,
            'end' => $request->end,
            'waypoints' => $request->waypoints
        ]);
        return redirect('/');
    } else {
        return redirect('/login');
    }   

}

并且正在show.blade中的数据库中显示

                <div id="dynamicInput" class="form-group">
                    <label>Additional Destinations</label>
                    <input type="text" name="waypoints" class="form-control" autocomplete="on" value="{{ $myroute->waypoints }}">
                </div>

                <input type="button" class="btn btn-secondary" value="+" onClick="addInput('dynamicInput');" style="padding:0 10px;">

由于航点在数据库中保存为字符串,如果路线有多个航点,则它会在一个输入字段中列出,如下图所示。 showing from database

它应该如下所示(这是用户创建并将路由添加到数据库的时间)。 adding to database

任何想法和帮助将不胜感激,因为我知道它非常复杂,谢谢!

【问题讨论】:

    标签: arrays database laravel google-maps google-maps-api-3


    【解决方案1】:

    使用json_encode($array) 将您的数组保存为字符串,然后通过json_decode($array) 检索它。

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 2019-10-31
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多