我将情感数据作为json存储在我的记录模型中(我正在加密记录表,不希望通过关系公开数据的潜力):
记录迁移
Schema::create('records', function (Blueprint $table) {
$table->id();
$table->text('title')->nullable();
$table->json('emotions')->nullable();
$table->timestamps();
});下面是我通过我的唱片资源获得的输出:
RecordResource
class RecordResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'emotions' => $this->emotions
];
}
}输出
"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对象中的数组中:
class Record extends Model
{
use HasFactory;
protected $casts = [
'emotions' => 'json',
];
public $emtionsLabels = ['Happy', 'Sad'];
}然后,我希望将标签与情感集合相匹配,并添加一个is_picked属性,以便输出如下所示:
新输出
"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输出,但是我遇到了很多问题:
试图求出非物体的性质“强度”
class EmotionResource extends JsonResource
{
public function toArray($request)
{
return ['intensity' => $this->intensity];
}
}更新1
在我的RecordController中,我成功地获得了我想要的情绪输出:
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中做到这一点。
发布于 2021-01-19 01:21:59
我使用上面更新的代码创建了一个访问器:
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);
}
}https://stackoverflow.com/questions/65770898
复制相似问题