Laravel - Method Illuminate\Support\Collection::makeHidden does not exist(Laravel - 方法 Illuminate\Support\Collection::makeHidden 不存在)
问题描述
我想隐藏列 password
&OTP
,包含在 $uses
结果中.实际上,这 2 列是 users
表的一部分.我试过如下.但它会产生错误 - Method Illuminate\Support\Collection::makeHidden does not exist
.如何解决这个问题?任何建议..
I want to hide the columns password
& OTP
,that is included in $uses
result. Actually these 2 columns are part of the users
table. I've tried like below. But it generates the error - Method Illuminate\Support\Collection::makeHidden does not exist
. How to solve this? Any suggestions..
$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);
推荐答案
您正在尝试在集合上执行此方法,但它是一个模型方法:
You're trying to execute this method on the collection but it's a model method:
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
foreach($users as $user) {
$user->makeHidden(['password','OTP']);
}
这仍然不起作用,因为您使用 DB::table('users')
而不是 Users::all()
.
And this still doesn't work since you're using DB::table('users')
over Users::all()
.
为了使用模型,您必须执行以下操作:
In order to use a model, you have to do the following:
型号:
class User extends Model
{
// Instead of `makeHidden()` you can do this if you want them always to be hidden
// protected $hidden = ['password','OTP'];
public function location()
{
return $this->hasOne(AppModelsLocation::class, 'users.id', '=', 'location.id');
}
public function technical_details()
{
return $this->hasOne(AppModelsUserTechnicalDetail::class, 'users.id', '=', 'user_technical_details.id');
}
}
控制器:
$users = Users::with(['location', 'technical_details'])
->get();
foreach($users as $user) {
$user->makeHidden(['password','OTP']);
}
这篇关于Laravel - 方法 Illuminate\Support\Collection::makeHidden 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel - 方法 Illuminate\Support\Collection::makeHidden 不存在
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01