在数据库Laravel中进行多选引导插入

时间:2019-04-14 12:56:46

标签: php mysql laravel eloquent relationship

我正在尝试创建多选引导程序输出以保存到数据库中,但是我正在像["6","7"]这样的防雷控制器中获取数组。

有2个选择,意味着必须创建2行。

查询我的想法:


$table = Pool_user::updateOrInsert(['pool_id' => [$string1], 'user_id' => $request['id']]);

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

通过直接回答您的问题,您需要使用要输入的值创建一个数组数组。

类似这样的东西:

<html>
<head>
<title>Test2.html</title>
    <script src="include/jquery-3.3.1.js"></script>
    <script src="include/moment.js"></script>
    <script src="include/daterangepicker.js"></script>
    <link rel="stylesheet" type="text/css" href="include/daterangepicker.css" />
</head>
<body>
<input type="text" name="datetimes"/>
Local <input type="radio" name="timeformat" value="local" checked="checked"><span id="mybar">|</span><input type="radio" name="timeformat" value="utc"> UTC

<script>

// Global Variables
var selectedtimeformat;

// Set the selectedtimeformat variable on page load
$( document ).ready(function() {
    setSelectedTimeFormat();
    attachpicker();
});

$("input[type='radio']").click(function(){
    var radioValue = $("input[name='timeformat']:checked").val();
        if(radioValue != selectedtimeformat){
            setSelectedTimeFormat();
            if(selectedtimeformat == 'utc'){
                $('input[name="datetimes"]').data('daterangepicker').remove();
                attachpicker();
            }else{
                // Re-attach the DRP so the startDate, endDate, minDate and maxDate settings are updated for the UTC/local time choice
                $('input[name="datetimes"]').data('daterangepicker').remove();
                attachpicker();
            }
        }       
});  // end function $("input[type='radio']").click(function(){...}

function setSelectedTimeFormat(){
    selectedtimeformat = $("input[name='timeformat']:checked").val();
    alert("selectedtimeformat: " + selectedtimeformat);
}

function attachpicker(){

if(selectedtimeformat == 'local'){
    $('input[name="datetimes"]').daterangepicker({
    "opens": 'right',
    "drops": 'down',
    "timePicker": true,
    "locale": {
      "format": 'MM/DD/YYYY'
    },
    "alwaysShowCalendars": true,    
    "ranges": {
        'Today': [moment(), moment()],
        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    },
    "startDate": "04/08/2019",
    "endDate": "04/10/2019",
    "minDate": "04/05/2019",
    "maxDate": "04/18/2019",
    "timePickerIncrement": 10,
    "timePicker24Hour": true
    }); // <-- end of daterangepicker() constructor
}else{
// selectedtimeformat == 'utc'
    $('input[name="datetimes"]').daterangepicker({
    "opens": 'right',
    "drops": 'down',
    "timePicker": true,
    "locale": {
      "format": 'MM/DD/YYYY'
    },
    "alwaysShowCalendars": true,
    "ranges": {
        'Today': [moment(), moment()],
        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    },
    "startDate": "04/08/2019",
    "endDate": "04/14/2019",
    "minDate": "04/05/2019",
    "maxDate": "04/18/2019",
    "timePickerIncrement": 10,
    "timePicker24Hour": true
    }); // <-- end of daterangepicker() constructor
}

}   //<-- end of function attachpicker()

$('input[name="datetimes"]').on('apply.daterangepicker', function(ev, picker) {
    alert("apply.daterangepicker event fired");
});

</script>

</body>
</html>

但是,考虑到$array = []; foreach($request['pool_ids'] as $pool_id){ //$request['pool_ids'] -> ["6","7"] array_push($array, [ 'pool_id' => $pool_id, 'user_id' => $request['id'] ]); } $table = Pool_user::insert($array); 是“多对多”关系的数据透视表,有一种更好的方法。 假设在用户模型中,有一个称为Pool_user的关系。

您可以简单地要求Eloquent同步关系:

polls()

希望这会有所帮助