【问题标题】:Saving Dynamic Select Values in database - Laravel在数据库中保存动态选择值 - Laravel
【发布时间】:2020-12-18 03:28:18
【问题描述】:

我正在以动态呈现的形式处理多个选择框。

在下面的场景中,我将选择映射到父标题。

示例结果为{ "1": [ 2 ], "2": [ 1, 3 ] }

        <table class="table">
          <thead>
            <tr>
              <td>Variation Name</td>
              <td>Variation Values</td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Size</td>
              <td>
                <select multiple="multiple">
                  <option value="2">Medium</option>
                </select>
              </td>
            </tr>
            <tr>
              <td>Color</td>
              <td>
                <select multiple="multiple">
                  <option value="1">White</option>
                  <option value="3">Blue</option>
                  <option value="4">Black</option>
                </select>
              </td>
            </tr>
          </tbody>
        </table>

我将结果传递给 Laravel 控制器,以便我可以保存响应..

我不确定如何将数据保存到数据库中..

public function itemsStore(Request $request)
    {
        $items_arrays = array($request['itemsArray'], true);
        dd(items_arrays);
    }

dd 结果是

array:2 [
  0 => "{"1":[2],"2":[1,3]}"
  1 => true
]

如何以相应的格式将值保存到数据库中

item_id | item_value_id
   1             2
   2             1
   2             3

我正在使用 Vue 填充上述对象。通过 axios 库将数据发送到控制器。 Fiddle

【问题讨论】:

  • 您是如何提交表单的,即您是使用 ajax 还是仅使用标准表单提交?

标签: php laravel vue.js eloquent vuejs2


【解决方案1】:

您需要为动态选择命名。 例如,如果您有一个 [size, color] 属性列表,您可以这样做

  • ...
  • ...

在服务器端你会收到一个数组参数 attr[]

$attr = request()->attr;

它应该看起来像 $attr['size'] => [你选择的尺寸] $attr['color'] => 你选择的颜色。

你可以做一个转储来观看表格。

希望对你有帮助,请告诉我。

【讨论】:

  • 感谢您的回复。这是我如何获取数据的小提琴。 jsfiddle.net/eurodgj7。任何增强将不胜感激
【解决方案2】:

试试这个替代方法

确保在您的 html 中将 name 属性设置为如下所示的数组

<select multiple="multiple" name="sizes[]" id="sizes">

然后要存储它,您可以像这样在控制器中获取它

$sizes = $request->input('sizes');//it will give you string separated by commas
$sizes = explode(',', $sizes);// to separate it explode the string

// save it to database

对于其他输入,例如多选颜色输入,您也可以使用上述步骤

【讨论】:

  • 感谢您的快速回复。我正在使用 Vue 来填充对象。我将上述对象存储到 itemsArray 同时传递给 laravel 控制器。这是小提琴jsfiddle.net/eurodgj7
【解决方案3】:

PHP 中,您有不同的方式来定义 array

一种方法是通过显式设置其值来定义它,例如:

<?php
  $my_array_1 = array("first" => 0, "second" => 1) // or;
  $my_array_2 = array(0, 1, 2);
?>

数组可以存储混合值,所以这是有效的:

<?php
  $my_array_3 = array(0, "one", 2)
?>

你的数组和之前的一样:

<?php
  $items_arrays = array($request['itemsArray'], true);
?>

PHP array() 没有参数,只有添加的项目,所以放在末尾的 true 只是数组的第二个元素。 https://www.php.net/manual/en/function.array.php

基于您的 dd 输出的正是您在定义数组时添加的内容:

array:2 [
  0 => "{"1":[2],"2":[1,3]}" // this is the string from $request['itemsArray']
  1 => true // this is the second element you added
]

我认为您的问题是您需要解析您在$request['itemsArray'] 中收到的string

  1. 那么,首先:
public function itemsStore(Request $request)
  {
    $json = json_decode($request['itemsArray']);
    // $json is now an associative array in PHP
    // something like: array(1 => array(2), 2 => array(1, 3))
    
    // $items_arrays = array($request['itemsArray'], true);
    // dd(items_arrays);
  }
  1. 那么你需要展平这个关联数组:
public function itemsStore(Request $request)
  {
    $json = json_decode($request['itemsArray']);
    // $json is now an associative array in PHP
    // something like: array(1 => array(2), 2 => array(1, 3))
    $items_arrays = [];
    foreach(json as $key1 => $val1) {
      foreach($val1 as $key2 => $val2) {
        $items_array[] = array($key1 => $val2);
      }
    }
    // $items_arrays = array($request['itemsArray'], true);
    dd(items_arrays);
  }

json_decode 有可选参数:https://www.php.net/manual/en/function.json-decode.php

当然,您应该在使用它之前检查$request - 身份验证和验证很重要!


抱歉,如果语法不正确 - 我已经有一年多没有使用 PHP 了,而是用心编写示例(没有检查)。但这个想法肯定是正确的:

  1. 从请求中获取数据(验证数据!流程不需要,但生产环境需要!)
  2. 将其从字符串格式转换为PHP数组
  3. 在最终的数组中读取它(最终需要它)
  4. 将最终格式放入数据库中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-04
    • 2020-03-15
    • 2016-04-08
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多