在数据库中保存动态选择值-Laravel [英] Saving Dynamic Select Values in database - Laravel

查看:82
本文介绍了在数据库中保存动态选择值-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working an multiple select Boxes in a form which are rendered dynamically.

在以下情况下,我将所选内容映射到父标题。

Here in the below scenario I am mapping the selecttion to the parent title.

示例结果为 { 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控制器,以便我可以保存响应。.

I am passing the result to the Laravel Controller so that I could save the response..

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

I am not sure how do I save the data to the database..

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
]

如何将值以各自的格式保存到数据库中

How do I save the values to database in the respective format

item_id | item_value_id
   1             2
   2             1
   2             3

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

I am populating the above object using Vue. Sending the data to controller through axios library. Fiddle

推荐答案

PHP 中,您可以使用不同的方法来定义数组

In PHP you have different ways to define an array.

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

One way is to define it by setting its values explicitly, like:

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

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

The arrays can store mixed values, so this is valid:

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

您的数组类似于之前的数组:

Your array is like the one before:

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

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

The PHP array() doesn't have a parameter only the items added, so the true you put at the end is just the second element of your array. https://www.php.net/manual/en/function.array.php

基于您的 dd 输出的结果与您定义数组时添加的内容完全相同:

Based on that your dd outputs exactly what you added when you defined the array:

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'] 中收到的字符串

I think your problem is that you need to parse the string that you receive in your $request['itemsArray'].


  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 具有可选参数:< a href = https://www.php.net/manual/zh/function.json-decode.php rel = nofollow noreferrer> https://www.php.net/manual/zh/function.json -decode.php )

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

Of course, you should check the $request before working with it - authentication & validation are important!

对不起,如果语法不正确-我已经使用PHP超过一年了,请全心全意地编写示例(不检查)。但是这个想法肯定是正确的:

Sorry, if the syntax is not correct - I haven't worked with PHP for over a year and wrote the samples by heart (no checking). But the idea is surely correct:


  1. 从请求中获取数据(验证数据!这对于流程不是必需的,但对于生产环境是必需的。 !)

  2. 将其从字符串格式转换为PHP数组

  3. 将其读入最终数组中(最后需要时使用)
  4. 将最终格式输入数据库

  1. Get the data from the request (Validate the data! This is not required for the process, but required for a production environment!)
  2. Transform it from the string format to PHP array
  3. Read it in a final array (as you need it in the end)
  4. Put the final format in the database

这篇关于在数据库中保存动态选择值-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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