我正在構建一個應用程序,其中有一列數據,我正在序列化以存儲在數據庫中,因為我找不到其他方法可以將數據存儲為一個對象。我的表單中數據是輸入復選框,如下所示。 <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
- 1 回答
- 0 關注
- 98 瀏覽
添加回答
舉報
0/150
提交
取消