1 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
你應(yīng)該使用雄辯的突變體來做到這一點(diǎn)。
在數(shù)據(jù)庫上使用 JSON 列,讓我們稱之為 ,然后在模型中執(zhí)行:careplan
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
// ...
protected $casts = [
'careplan' => 'array',
];
// ...
}
所以現(xiàn)在,你將使用一個(gè)元素,不再需要從數(shù)據(jù)庫中手動(dòng)序列化/取消序列化它。Laravel 將在存儲(chǔ)時(shí)將 轉(zhuǎn)換為 ,并將其轉(zhuǎn)換回從數(shù)據(jù)庫獲取值時(shí)。所以現(xiàn)在: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();
然后你會(huì)看到:
$patient = Patient::find(1);
dd($patient->careplan);
已轉(zhuǎn)換回 。array
- 1 回答
- 0 關(guān)注
- 102 瀏覽
添加回答
舉報(bào)