我试图在返回 api 响应之前删除created_at和updated_at。我想从Place_Type 和 Places中删除这些字段 ,我该怎么做?:我尝试过:unset(placetypes) 但它不起作用
这是我的代码:
public function places()
{
$placeType = PlaceType::with('places')->where('id', 1)->get();
return response()->json(['placeType' => $placeType]);
}
Run Code Online (Sandbox Code Playgroud)
请求结果:
"placeType": [
{
"id": 1,
"name": "Moriah O'Conner",
"icon": "https://picsum.photos/200/300",
"status": 1,
"created_at": "2019-12-14 18:23:19",
"updated_at": "2019-12-14 18:23:19",
"places": [
{
"id": 1,
"name": "Linda Leffler",
"description": "Alice's shoulder, and it set to work, and very soon came to ME, and told me he was in the air. She did it so VERY remarkable in that; …Run Code Online (Sandbox Code Playgroud) 我有一个 VueJs 项目作为前端,一个 Laravel 项目作为后端,用于制作 api 和服务器。
我在 laravel 中使用jwt,它返回包含用户信息的 jwt 令牌。
问题:
我需要将此令牌存储在带有 HTTPOnly 标志的 cookie 中,但我不知道在哪里以及如何设置此 cookie!?在服务器端(Laravel)?在客户端(VueJs)?
代码:
我在 Laravel 中的 AuthController:
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
Run Code Online (Sandbox Code Playgroud)
它返回:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC92dWV4LmNhY3R1c3dlYi5pclwvYXBpXC9sb2dpbiIsImlhdCI6MTU3MzkxNDA1NSwiZXhwIjoxNTczODQ3NTQ2YWEiLCJ1c2VyIjp7ImlkIjozLCJuYW1lIjoiYWxpIiwiZW1haWwiOiJhQGEuY29tIiwicm9sZSI6IjIiLCJlbWFpbF92ZXJpZmllZF9hdCI6bnVsbCwiY3JlYXRlZF9hdCI6IjIwMTktMTEtMTQgMDg6MDU6NDUiLCJ1cGRhdGVkX2F0IjoiMjAxOS0xMS0xNCAwODowNTo0NSJ9fQ.Ow785CLmaAckZR9iowVkEMX6AxBQw7JSklt3Vp1btAcG4",
"token_type": "bearer",
"expires_in": 3600
}
Run Code Online (Sandbox Code Playgroud)
在我的 Vuex …
我有一个数组,它用 js 在前端制作,然后用 ajax 将它传递给我的控制器。
阿贾克斯:
var values = [{FirstName: "fff"},{LastName: null}]
$.ajax({
method: "POST",
url: "/api/store-step",
data: { values: values, step: activePanelNum }
}).fail(function (jqXHR, textStatus, error,result) {
console.log(jqXHR.responseJSON.errors);
}).done(function( result ) {
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
数组的结构是这样的:
[{FirstName: "fff"},{LastName: null}]
Run Code Online (Sandbox Code Playgroud)
控制器:
public function storeSteps(Request $request)
{
$validator = Validator::make($request->values, [
'FirstName' => 'required',
'LastName' => 'required',
]);
if ($validator->fails()) {
return response()->json(['success'=>false, 'errors' => $validator->getMessageBag()->toArray()],422);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法使用请求验证 Laravel 来验证此数组。现在我要把这个数组变成一个 Larval 请求来应用规则。
任何人都可以帮忙吗?