1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以使用Mutators來實(shí)現(xiàn)這一點(diǎn),如下所示:
class MyModel extends Model
{
? ? public function setLanguageMetaAttribute(array $meta)
? ? {
? ? ? ? $this->attributes['language_meta'] = json_encode($meta); // Store as json encoded string
? ? }
? ? public function getLanguageMetaAttribute(string $meta)
? ? {
? ? ? ? return json_decode($meta, true); // Return as associative php array
? ? }
}
現(xiàn)在你可以像這樣使用它:
? ? $model = MyModel::find($some_id); // Or however you want to select
? ? $model->language_meta = [
? ? ? ? "meta_page_title_cc__c" => "Historic villa for sale within moments of central Florence"
? ? ? ? "meta_page_title_fr__c" => null
? ? ? ? "meta_page_title_it_cc__c" => "Villa prestigiosa in vendita situata appena fuori il centro storico di"
? ? ? ? "meta_page_title_ru_cc__c" => null
? ? ? ? "meta_description_it_cc__c" => "Situata in uno dei sobborghi residenziali più prestigiosi di Firenze, questa straordinaria villa storica in vendita è una rara scoperta architettonica che unisce al suo interno magnifiche caratteristiche originali."
? ? ? ? "meta_description_fr_cc__c" => null
? ? ? ? "meta_description_ru_cc__c" => null
? ? ];
? ??
? ? $model->save(); // Persists in DB as json
當(dāng)你去檢索它時(shí),它已經(jīng)是一個(gè)數(shù)組,所以你可以這樣做:
? ? $model = MyModel::find($some_id); // Or however you want to select
? ? foreach ($model->language_meta as $key => $value) {
? ? ? ? // Whatever you want to do with the meta here
? ? }
- 1 回答
- 0 關(guān)注
- 147 瀏覽
添加回答
舉報(bào)