php - Laravel - 隐藏特定列
问题描述
我想隐藏包含在s 结果中的列password
& 。实际上,这 2 列是 users 表的一部分。我的最终需要是我需要加入 3 个表:,,并希望隐藏用户表中的&列。可以使用任何方法。通过任何方法,我想达到这个结果我尝试了很多方法。没有任何效果。如何解决这个问题?有什么建议么..OTP
$user
users
location
user_technical_details
password
OTP
我尝试过的事情:
1)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$d=$users->makeHidden(['password','OTP']);
return response()->json([
'message' => 'profile viewed successfully',
'data' => $d,
'statusCode' => 200,
'status' => 'success'],200);
这会产生错误 -Method Illuminate\\Support\\Collection::makeHidden does not exist
2)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$exclude_columns=['password','OTP'];
$get_columns = array_diff($users, $exclude_columns)->get();
return response()->json([
'message' => 'profile viewed successfully',
'data' => $get_columns,
'statusCode' => 200,
'status' => 'success'],200);
3)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$d=collect($users->toArray())->except(['password','OTP']);
return response()->json([
'message' => 'profile viewed successfully',
'data' => $d,
'statusCode' => 200,
'status' => 'success'],200);
4)
protected $hidden = ['密码','OTP'];
5)
$users = DB::table('users')->exclude(['password','OTP','ph_OTP','email_OTP','user_access_token','remember_token'])
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
return response()->json([
'message' => 'profile viewed successfully',
'data' => $users,
'statusCode' => 200,
'status' => 'success'],200);
这会产生错误 -Call to undefined method Illuminate\\Database\\Query\\Builder::exclude()
解决方案
当您想要限制包含在模型数组或 JSON 表示中的属性(例如密码)时。为此,请将$hidden属性添加到您的模型中。在 $hidden 属性的数组中列出的属性将不会包含在模型的序列化表示中:
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password','OTP'];
}
现在在您的代码中,您必须使用用户模型而不是 DB 外观:
$users = User::query()
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
现在,$users
不会有隐藏属性
推荐阅读
- c++ - 我的 IDE 或代码有问题吗?
- neo4j - Neo4J - 根据某些不存在的属性获取过滤结果
- python - 如何根据多个条件用字符串拆分 Pandas 数据框列
- javascript - 查询特定词节点js snoowrap
- postgresql - Ubuntu 上的 PostgreSQL - 这些僵尸进程是什么以及导致它们停留太长时间的原因
- rust - 无法在提交 43a74718 处编译 Servo crate:在 `webrender_api` 中找不到`Transaction`
- html - 如何将背景图片放在两个 div 前面?
- ios - Mac Catalyst 无法将图像保存在相册中,但不会产生错误
- sql - 具有默认值的 oracle sql 聚合函数
- php - 根据产品 ID 更改 WooCommerce 结帐订单备注占位符和标签