数组作为字符串保存到数据库,现在需要将字符串过帐到数据库的单独输入字段 - Laravel [英] Array is saved to database as string, now need to post string into separate input fields from database - Laravel

查看:656
本文介绍了数组作为字符串保存到数据库,现在需要将字符串过帐到数据库的单独输入字段 - Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是一个相当复杂的问题。从我的最后一个问题来看,我展示了如何将数组保存到数据库。
" Array to字符串转换 errorr Laravel - 尝试将数组保存到数据库中 - 我可能必须更改它保存到数据库的方式才能正确显示。



我正在使用谷歌地图ara在laravel用户可以创建一个路线,保存它(到数据库),并在他们登录后可以再次查看并编辑或删除。



航点功能可以工作,所以点击+按钮后会出现更多的输入字段 - 这是用户添加的方式。



在shows.blade中的航点功能

p>

  var counter = 1; 
var limit = 10;
var i = 0;
函数addInput(divName){
if(counter == limit){
alert(您已达到添加+ counter +其他目的地的限制);
} 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']});


$ b code
$ b

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

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

Myroutes :: create([//发布到acc表
'user_id'=> Auth :: user() - > id,
'start'=> $ request-> start,
'end'=> $ request-> end,
'waypoints'=> $ request-> waypoints
]);
返回重定向('/');
} else {
return redirect('/ login');
}

}

数据库显示.blade

 < div id =dynamicInputclass =form-group> 
< label>其他目的地< / label>
< input type =textname =waypointsclass =form-controlautocomplete =onvalue ={{$ myroute-> waypoints}}>
< / div>

< input type =buttonclass =btn btn-secondaryvalue =+onClick =addInput('dynamicInput'); style =padding:0 10px;>

由于航点是作为字符串保存在数据库中的,因此如果航点有多个航点,一个输入字段,如下图所示。
显示数据库



当它应该看起来如下(这是用户创建并将路由添加到数据库时)。
添加到数据库



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

解决方案

保存你的数组作为字符串与 json_encode($ array),然后检索它 json_decode($ array)


So this is quite a complicated question. Following on from my last question which shows how I am saving an array to the database. "Array to string conversion" errorr Laravel - Trying to save array to database - I might have to change how it saves to the database to get it to show correctly.

I am using the google maps api in laravel where a user can create a route, save it (to the database) and when they are logged in can view it again and edit or delete.

The waypoints function works, so on click of the + button more input fields appear - This is how the user adds them in.

waypoints function in 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']});

              }
            }

It saves to the database as shown in 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');
    }   

}

And is showing from database in 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;">

Since the waypoints are saved as a string in the database, if the route has multiple waypoints its listing in one input field as shown in the image below. showing from database

When it should look as follows (this is when the user creates and adds the route to database). adding to database

Any ideas and help on this will be greatly appreciated as I know its pretty complicated, thanks!

解决方案

Save your array as a string with json_encode($array) and then retrieve it json_decode($array).

这篇关于数组作为字符串保存到数据库,现在需要将字符串过帐到数据库的单独输入字段 - Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆