如何在用户通过身份验证时在 API 响应中包含额外数据
How to include extra data in API response when user is authenticated
我正在创建一个用户可以使用闪存卡学习的项目。我有一个 API,用户可以从中检索一副牌。未通过身份验证的用户仍然可以检索套牌,但他们的学习进度不会存储在数据库中。
我希望经过身份验证的用户在从 API 检索闪存卡时在响应中接收额外数据。更具体地说,每张卡片都应该有一个额外的 'study_at' 字段,其中包含他们接下来应该学习该卡片的日期。
'study_at' 日期存储在名为 'card_user' 的枢轴 table 中。
我尝试修改 Card 模型上的 toArray() 方法,但我不知道那是否可行。
这是 API 端点:
Route::get('/decks', 'DecksController@index');
DecksController
class DecksController extends Controller
{
public function index()
{
$decks = Deck::all();
return new DeckCollection($decks);
}
牌组收藏
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,
];
}
}
卡片收藏
class CardCollection extends ResourceCollection
{
public function toArray($request)
{
return parent::toArray($request);
}
}
卡片资源
这里是我要包含 '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"
}
您可以定义关系 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,
];
}
}
要仅在特定条件下包含属性,您应该使用 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,
];
}
}
我正在创建一个用户可以使用闪存卡学习的项目。我有一个 API,用户可以从中检索一副牌。未通过身份验证的用户仍然可以检索套牌,但他们的学习进度不会存储在数据库中。
我希望经过身份验证的用户在从 API 检索闪存卡时在响应中接收额外数据。更具体地说,每张卡片都应该有一个额外的 'study_at' 字段,其中包含他们接下来应该学习该卡片的日期。
'study_at' 日期存储在名为 'card_user' 的枢轴 table 中。
我尝试修改 Card 模型上的 toArray() 方法,但我不知道那是否可行。
这是 API 端点:
Route::get('/decks', 'DecksController@index');
DecksController
class DecksController extends Controller
{
public function index()
{
$decks = Deck::all();
return new DeckCollection($decks);
}
牌组收藏
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,
];
}
}
卡片收藏
class CardCollection extends ResourceCollection
{
public function toArray($request)
{
return parent::toArray($request);
}
}
卡片资源
这里是我要包含 '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"
}
您可以定义关系 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,
];
}
}
要仅在特定条件下包含属性,您应该使用 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,
];
}
}