首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将json集合id与模型中的数组匹配,并添加相应的标签。

将json集合id与模型中的数组匹配,并添加相应的标签。
EN

Stack Overflow用户
提问于 2021-01-18 08:18:10
回答 1查看 41关注 0票数 0

我将情感数据作为json存储在我的记录模型中(我正在加密记录表,不希望通过关系公开数据的潜力):

记录迁移

代码语言:javascript
复制
Schema::create('records', function (Blueprint $table) {
    $table->id();
    $table->text('title')->nullable();
    $table->json('emotions')->nullable();
    $table->timestamps();
});

下面是我通过我的唱片资源获得的输出:

RecordResource

代码语言:javascript
复制
class RecordResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'emotions' => $this->emotions
        ];
    }
}

输出

代码语言:javascript
复制
"data":[
   {
      "id":91,
      "title":"Temporibus sed ut voluptas nesciunt.",
      "emotions":[
         {
            "intensity":10,
            "emotion_id":1,
            "newIntensity":3
         },
         {
            "intensity":10,
            "emotion_id":2,
            "newIntensity":3
         }
      ]
   },
   {
      "id":92,
      "title":"Facilis provident qui tempore sit illum fuga incidunt odio.",
      "emotions":[
         {
            "intensity":10,
            "emotion_id":1,
            "newIntensity":3
         },
         {
            "intensity":10,
            "emotion_id":2,
            "newIntensity":3
         }
      ]
   }
]

我想要做的是将情感标签存储在DB中,或者暂时存储在records对象中的数组中:

代码语言:javascript
复制
class Record extends Model
{
    use HasFactory;

    protected $casts = [
        'emotions' => 'json',
    ];

    public $emtionsLabels = ['Happy', 'Sad'];
}

然后,我希望将标签与情感集合相匹配,并添加一个is_picked属性,以便输出如下所示:

新输出

代码语言:javascript
复制
"data":[
   {
      "id":91,
      "title":"Temporibus sed ut voluptas nesciunt.",
      "emotions":[
         {
            "intensity":10,
            "emotion_id":1,
            "newIntensity":3
            "label": "Happy",
            "is_picked": false
         },
         {
            "intensity":10,
            "emotion_id":2,
            "newIntensity":3
            "label": "Sad",
            "is_picked": false
         }
      ]
   },
   {
      "id":92,
      "title":"Facilis provident qui tempore sit illum fuga incidunt odio.",
      "emotions":[
         {
            "intensity":10,
            "emotion_id":1,
            "newIntensity":3
            "label": "Happy",
            "is_picked": false
         },
         {
            "intensity":10,
            "emotion_id":2,
            "newIntensity":3
            "label": "Sad",
            "is_picked": false
         }
      ]
   }
]

我本打算尝试把情感--它是自己的资源-- 'emotions' => EmotionResource::collection(json_decode($this->emotions, true)),这样我就可以在那里构造json输出,但是我遇到了很多问题:

试图求出非物体的性质“强度”

代码语言:javascript
复制
class EmotionResource extends JsonResource
{
    public function toArray($request)
    {
        return ['intensity' => $this->intensity];
    }
}

更新1

在我的RecordController中,我成功地获得了我想要的情绪输出:

代码语言:javascript
复制
class RecordController extends Controller
{
    public function index()
    {

        $emotions = Record::first()->emotions;

        $phpRecords = json_decode($emotions, true);

        $newEmotions;
        $record = new Record();
        $labels = $record->emtionsLabels;
        foreach($phpRecords as $emotion){
            $newEmotions[] = 
                [
                    'intensity' => $emotion['intensity'],
                    'new_intensity' => $emotion['newIntensity'],
                    'emotion_id'=> $emotion['emotion_id'],
                    'label' => $labels[$emotion['emotion_id'] - 1]
                ];
        }

        return collect($newEmotions);

    }
}

我只需要弄清楚如何在EmotionResource或RecordResource中做到这一点。

EN

回答 1

Stack Overflow用户

发布于 2021-01-19 01:21:59

我使用上面更新的代码创建了一个访问器:

代码语言:javascript
复制
class Record extends Model
{
    use HasFactory;

    /*protected $casts = [
        'emotions' => 'json',
    ];*/

    public $emtionsLabels = ['Happy', 'Sad'];

    public function getEmotionsAttribute($value)
    {
        $emotions = json_decode($value, true);
        foreach($emotions as $emotion){
            $newEmotions[] = 
                [
                    'intensity' => $emotion['intensity'],
                    'new_intensity' => $emotion['newIntensity'],
                    'emotion_id'=> $emotion['emotion_id'],
                    'label' => $this->emtionsLabels[$emotion['emotion_id'] - 1]
                ];
        }

        return collect($newEmotions); 
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65770898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档