'Laravel - 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);  


Solution 1:[1]

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']);
}

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:

model:

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(App\Models\Location::class, 'users.id', '=', 'location.id');
    }

    public function technical_details()
    {
        return $this->hasOne(App\Models\UserTechnicalDetail::class, 'users.id', '=', 'user_technical_details.id');
    }
}

controller:

$users = Users::with(['location', 'technical_details'])
            ->get();
foreach($users as $user) {
    $user->makeHidden(['password','OTP']);
}

Solution 2:[2]

Simplest solution for your case:

$users = DB::table('users')
        ->join('location', 'users.id', '=', 'location.id')
        ->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
        ->get()
        ->map(function ($user) {
            unset($user->password);
            unset($user->OTP);
            reurn $user;
        });

I'd recommend to use Eloquent Relationships instead of joins for better abstraction:

https://laravel.com/docs/8.x/eloquent-relationships

Solution 3:[3]

In order to get User object from db, you should use User Model:

$users = User::query()
            ->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']);
}

Solution 4:[4]

I'm late to the party, sorry. I had the same issue. I wanted to exclude created_at and updated_at columns from my SQL result (using DB:Table, not specific eloquent models). The makeHidden function "does not exist". NoOorz24 has a similar answer to mine, but instead, I did this (which is useful if you're running a slightly old version of PHP). It's a bit more processor hungry but it did the job.

 $content = DB::table($talbename)->get();
 foreach($content as $c) {
     unset($c->created_at);
     unset($c->updated_at);
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 NoOorZ24
Solution 3 OMR
Solution 4 James