Laravel-Method Lighmate\Support\Collection::Make Hidden不存在
我要隐藏
password
&;OTP
列,它们包含在$uses
结果中。实际上,这两列是users
表的一部分。我已经试过了,如下所示。但是它会生成错误-Method Illuminate\Support\Collection::makeHidden does not exist
。如何解决这个问题呢?任何建议..
$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);
解决方案
您试图对集合执行此方法,但它是模型方法:
$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()
。
要使用模型,您必须执行以下操作:
型号:
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']);
}
相关文章