| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Http\Resources;
- use Illuminate\Http\Resources\Json\JsonResource;
- use App\Http\Api\UserApi;
- use App\Http\Api\ChannelApi;
- use App\Models\DhammaTerm;
- class RelationResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * The payload must contain only scalars and arrays: it is cached by
- * RelationController, and Laravel 13 refuses to unserialize objects from
- * cache unless they are listed in cache.serializable_classes. Hence the
- * associative json_decode() and the ISO-8601 date strings.
- *
- * @param \Illuminate\Http\Request $request
- * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
- */
- public function toArray($request)
- {
- $data = [
- "id" => $this->id,
- "name" => $this->name,
- "case" => $this->case,
- "from" => json_decode((string) $this->from, true),
- "to" => json_decode((string) $this->to, true),
- "match" => json_decode((string) $this->match, true),
- "category" => $this->category,
- "created_at" => $this->created_at?->toISOString(),
- "updated_at" => $this->updated_at?->toISOString(),
- ];
- if (!$request->has('vocabulary')) {
- $data["editor"] = UserApi::getByUuid($this->editor_id);
- $uiLang = strtolower($request->input('ui-lang', 'zh-Hans'));
- $term_channel = ChannelApi::getSysChannel("_System_Grammar_Term_{$uiLang}_");
- if ($term_channel) {
- $data['category_channel'] = $term_channel;
- if (!empty($this->category)) {
- $term = DhammaTerm::where("word", $this->category)
- ->where('channal', $term_channel)
- ->first();
- if ($term) {
- $data['category_term']['channelId'] = $term_channel;
- $data['category_term']['word'] = $term->word;
- $data['category_term']['id'] = $term->guid;
- $data['category_term']['meaning'] = $term->meaning;
- }
- }
- $data['name_channel'] = $term_channel;
- $term_name = DhammaTerm::where("word", $this->name)
- ->where('channal', $term_channel)
- ->first();
- if ($term_name) {
- $data['name_term']['channelId'] = $term_channel;
- $data['name_term']['word'] = $term_name->word;
- $data['name_term']['id'] = $term_name->guid;
- $data['name_term']['meaning'] = $term_name->meaning;
- }
- }
- }
- /*
- */
- return $data;
- }
- }
|