亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

在 laravel 5 視圖中取消序列化一列數據的正確方法是什么

在 laravel 5 視圖中取消序列化一列數據的正確方法是什么

PHP
忽然笑 2022-08-05 15:52:23
我正在構建一個應用程序,其中有一列數據,我正在序列化以存儲在數據庫中,因為我找不到其他方法可以將數據存儲為一個對象。我的表單中數據是輸入復選框,如下所示。    <div class="form-group form-check col-sm-6">        </br>        <label for="careplan">Care Plan</label></br>        <input type ="checkbox" name="careplan[]" value="nursecallweekly">   Nurse call weekly</br>        <input type ="checkbox" name="careplan[]" value="nursecallbiweekly">   Nurse call bi-monthly</br>        <input type ="checkbox" name="careplan[]" value="nursecallmonthly">   Nurse call monthly</br>        <input type ="checkbox" name="careplan[]" value="weightcheckdaily">   Weight check daily</br>        <input type ="checkbox" name="careplan[]" value="weightcheckweekly">   Weight check weekly</br>        <input type ="checkbox" name="careplan[]" value="lowerextremity">   Lower extremity edema status</br>        <input type ="checkbox" name="careplan[]" value="dietaryreview">   Dietary review</br>        <input type ="checkbox" name="careplan[]" value="fluidintake">   Fluid intake review</br>        <input type ="checkbox" name="careplan[]" value="bloodpressure">   Blood pressure reading daily</br>        <input type ="checkbox" name="careplan[]" value="bloodpressureweekly">   Blood pressure reading weekly</br>        <input type ="checkbox" name="careplan[]" value="hypertensive">   Hypertensive symptoms check</br>        <input type ="checkbox" name="careplan[]" value="ptinrweekly">   PT/INR weekly review</br>    </div>護理計劃有多個項目可供選擇。我將它們分組到一個數組中,并使用序列化數組。    //Create new Patient information    $patient = new Patient;    $patient->first_name = $request->input('first_name');    $patient->last_name = $request->input('last_name');    $patient->dob = $request->input('dob');    $patient->contact_one = $request->input('contact_one');    $patient->contact_two = $request->input('contact_two');    $patient->care_giver_one = $request->input('care_giver_one');    $patient->care_giver_two = $request->input('care_giver_two');頂部的圖像是視圖。如您所見,將返回序列化數據。我已經瀏覽了這個論壇,沒有一個發布的建議有效。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

你應該使用雄辯的突變體來做到這一點。


在數據庫上使用 JSON 列,讓我們稱之為 ,然后在模型中執行:careplan


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Patient extends Model

{

    // ...


    protected $casts = [

        'careplan' => 'array',

    ];


    // ...

}

所以現在,你將使用一個元素,不再需要從數據庫中手動序列化/取消序列化它。Laravel 將在存儲時將 轉換為 ,并將其轉換回從數據庫獲取值時。所以現在:arrayarrayjsonarray


$patient = new Patient;

$patient->first_name = $request->input('first_name');

// ...

$patient->careplan = $request->input('careplan'); // Laravel will convert it to json for you

$patient->save();

然后你會看到:


$patient = Patient::find(1);

dd($patient->careplan);

已轉換回 。array


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 98 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號