Laravel-Method Lighmate\Support\Collection::Make Hidden不存在 [英] Laravel - Method Illuminate\Support\Collection::makeHidden does not exist

查看:68
本文介绍了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']);
}

这篇关于Laravel-Method Lighmate\Support\Collection::Make Hidden不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆