第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在數(shù)據(jù)庫中保存動態(tài)選擇值 - Laravel

在數(shù)據(jù)庫中保存動態(tài)選擇值 - Laravel

PHP
動漫人物 2023-11-03 21:32:23
我正在以動態(tài)呈現(xiàn)的形式處理多個選擇框。在下面的場景中,我將選擇映射到父標(biāo)題。示例結(jié)果是 { "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>我將結(jié)果傳遞給 Laravel 控制器,以便我可以保存響應(yīng)。我不確定如何將數(shù)據(jù)保存到數(shù)據(jù)庫..public function itemsStore(Request $request)    {        $items_arrays = array($request['itemsArray'], true);        dd(items_arrays);    }結(jié)果dd是array:2 [  0 => "{"1":[2],"2":[1,3]}"  1 => true]如何以相應(yīng)的格式將值保存到數(shù)據(jù)庫item_id | item_value_id   1             2   2             1   2             3我使用 Vue 填充上述對象。通過 axios 庫將數(shù)據(jù)發(fā)送到控制器。 小提琴
查看完整描述

3 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個贊

在PHP中,您有不同的方法來定義array.


一種方法是通過顯式設(shè)置其值來定義它,例如:


<?php

  $my_array_1 = array("first" => 0, "second" => 1) // or;

  $my_array_2 = array(0, 1, 2);

?>

數(shù)組可以存儲混合值,因此這是有效的:


<?php

  $my_array_3 = array(0, "one", 2)

?>

你的數(shù)組就像之前的數(shù)組一樣:


<?php

  $items_arrays = array($request['itemsArray'], true);

?>

PHP沒有參數(shù),只有添加的項目,因此您放在末尾的只是數(shù)組的第二個元素 。 https://www.php.net/manual/en/function.array.phparray()true


基于此,您的dd輸出與定義數(shù)組時添加的內(nèi)容完全相同:


array:2 [

  0 => "{"1":[2],"2":[1,3]}" // this is the string from $request['itemsArray']

  1 => true // this is the second element you added

]

我認(rèn)為你的問題是你需要解析string你在$request['itemsArray'].


所以,首先:

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);

  }

然后你需要展平這個關(guān)聯(lián)數(shù)組:

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有可選參數(shù):https://www.php.net/manual/en/function.json-decode.php)


當(dāng)然,您應(yīng)該$request在使用它之前檢查 - 身份驗(yàn)證和驗(yàn)證很重要!

抱歉,如果語法不正確 - 我已經(jīng)一年多沒有使用 PHP 了,并且用心編寫了示例(沒有檢查)。但這個想法肯定是正確的:

  1. 從請求中獲取數(shù)據(jù)(驗(yàn)證數(shù)據(jù)!這不是流程所必需的,但生產(chǎn)環(huán)境需要?。?/p>

  2. 將其從字符串格式轉(zhuǎn)換為 PHP 數(shù)組

  3. 在最終數(shù)組中讀取它(因?yàn)槟罱K需要它)

  4. 將最終的格式存入數(shù)據(jù)庫


查看完整回答
反對 回復(fù) 2023-11-03
?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個贊

您需要為動態(tài)選擇指定一些名稱。例如,如果您有一個屬性列表 [尺寸,顏色],您可以執(zhí)行以下操作

  • ...

  • ...

在服務(wù)器端您將收到一個數(shù)組 param attr[]

$attr = 請求()->attr;

它應(yīng)該看起來像 $attr['size'] => [ 您選擇的尺碼] $attr['color'] => 您選擇的顏色。

您可以轉(zhuǎn)儲來觀看表格。

我希望它有幫助,請告訴我。


查看完整回答
反對 回復(fù) 2023-11-03
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個贊

嘗試這個替代方案


確保在 html 中將 name 屬性設(shè)置為如下所示的數(shù)組


<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

同樣,對于其他輸入(例如多選顏色輸入),您可以使用上述步驟


查看完整回答
反對 回復(fù) 2023-11-03
  • 3 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號