1 回答

TA貢獻1802條經驗 獲得超10個贊
您可以使用Mutators來實現這一點,如下所示:
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
? ? }
}
現在你可以像這樣使用它:
? ? $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
當你去檢索它時,它已經是一個數組,所以你可以這樣做:
? ? $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 關注
- 145 瀏覽
添加回答
舉報