验证用户身份后如何在API响应中包括额外数据

时间:2019-04-17 15:40:07

标签: laravel laravel-5.8

我正在创建一个项目,用户可以在其中使用闪存卡进行学习。我有一个API,用户可以从中检索卡片组。未通过身份验证的用户仍然可以检索卡片组,但是他们的学习进度不会存储在数据库中。

我希望经过身份验证的用户从API检索闪存卡时在响应中接收到额外的数据。更具体地说,每张卡都应该有一个额外的“ study_at”字段,其中包含他们下次应该学习该卡的日期。

“ study_at”日期存储在名为“ card_user”的数据透视表中。

我试图修改Card模型上的toArray()方法,但我不知道这是否可行。

这是API端点:

Route::get('/decks', 'DecksController@index');

DecksController

class DecksController extends Controller
{
    public function index()
    {
        $decks = Deck::all();

        return new DeckCollection($decks);
    }

DeckCollection

class DeckCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

DeckResource

class DeckResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'cards' => new CardCollection($this->cards),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

CardCollection

class CardCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

CardResource

我要在此处添加“ study_at”日期

class CardResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'front' => $this->front,
            'back' => $this->back,
            // Include 'study_at' date here
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

这是我希望经过身份验证的用户收到的答复:

{
    "id": 1,
    "name": "Coffee Break French",
    "cards": [
        {
            "id": 1,
            "front": "Good morning",
            "back": "Bonjour",
            "study_at": "2019-05-26T15:00:00.000000Z"
            "created_at": "2019-04-14T21:04:05.000000Z",
            "updated_at": "2019-04-14T21:04:05.000000Z"
        },
        {
            "id": 2,
            "front": "Good afternoon",
            "back": "Bonne après-midi",
            "study_at": "2019-05-26T15:00:00.000000Z"
            "created_at": "2019-04-14T21:04:21.000000Z",
            "updated_at": "2019-04-14T21:04:21.000000Z"
        },
        {
            "id": 3,
            "front": "My name is John",
            "back": "Je m'appelle John",
            "study_at": "2019-05-26T15:00:00.000000Z"
            "created_at": "2019-04-14T21:04:37.000000Z",
            "updated_at": "2019-04-14T21:04:37.000000Z"
        }
    ],
    "created_at": "2019-04-14T21:03:38.000000Z",
    "updated_at": "2019-04-14T21:03:38.000000Z"
}

2 个答案:

答案 0 :(得分:4)

要仅在特定条件下包括属性,应使用conditional attributes

class CardResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'front' => $this->front,
            'back' => $this->back,
            // Include 'study_at' date here
            'study_at' => $this->when(auth()->user(), $this->study_at), // Retrieve your value through eloquent relation
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

答案 1 :(得分:0)

您可以定义与Laravel Eloquent的关系:

class Card extends Model
{
   public function userCard() {
     // your relationship
   }
}
class CardResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'front' => $this->front,
            'back' => $this->back,
            'study_at' => $this->userCard,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

请参阅: https://laravel.com/docs/5.8/eloquent-relationships