我收到的某些值的回复无效,我希望用双引号来获取它,就像我的回复中的其他回复一样:
{
"status": "true",
"message": "User Logged-in Successfully!",
"dataArray": {
"id": 110,
"contacts_id": 12,
"email": "helloalisha123452@hotmail.com",
"companyID": 2,
"isDeleted": "N",
"isActive": "Y",
"Lastlogin": "0000-00-00 00:00:00",
"memberTypeID": 2,
"pricing_plan_id": 1,
"can_trail": "Y",
"first_login": "Y",
"confirmation": "Y",
"created_at": "2016-06-14 13:30:31",
"updated_at": "2016-06-14 13:31:11"
}
}
我们可以看到我在contacts_id中有无效的响应,这在12中是简单的,它应该是双引号,就像其他值一样
和来自响应来源的控制器代码:
public function userlogin(Guard $auth, Request $request)
{
$isVerified = $auth->attempt($request->only('email', 'password'), true);
if ($isVerified) {
$user = $auth->user()->toArray();
$responseToReturn = [
'status' => 'true',
'message' => 'User Logged-in Successfully!',
'dataArray' => $user
];
return Response::json($responseToReturn, 200);
} else {
$responseToReturn = [
'status' => 'false',
'message' => 'Invalid Email / Password',
];
return Response::json($responseToReturn, 200);
}
}
我只想用双引号获取我的值,我怎么做呢
答案 0 :(得分:1)
您可以在雄辩的模型中使用变异器或attribute casting。
模型上的$ casts属性提供了一种方便的方法 将属性转换为通用数据类型。
在您的情况下,您应该执行以下操作:
class User extends Model {
...
protected $casts = [
'companyID' => 'string',
'memberTypeID' => 'string',
...
];
...
}
如果您不想使用雄辩模型,可以创建一个这样的帮助:
class MyHelper extends Helper
{
public static function respondWithStrings($arr, $code = 200)
{
return response()->json(
array_map('strval', $arr),
$code
);
}
}
并随时使用它。例如:
class MyController extends Controller {
public function index()
{
$array = [
'example' => 1,
];
return MyHelper::respondWithStrings($array);
}
}