Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does laravel-mongodb respect $hidden? #1119

Closed
rohitsodhia opened this issue Feb 16, 2017 · 7 comments · Fixed by #2687
Closed

Does laravel-mongodb respect $hidden? #1119

rohitsodhia opened this issue Feb 16, 2017 · 7 comments · Fixed by #2687
Labels
bug Needs investigation Need investigation about bugs described in issue

Comments

@rohitsodhia
Copy link

I'm having trouble figuring this out in the code, and I'm not sure if maybe I'm doing it wrong, but I have fields I'd like to add to a model that I don't want saved into the final document. Eloquent usually uses $hidden for this kind of stuff, but it doesn't seem to work here. Am I missing something?

@coreywelch
Copy link

coreywelch commented Mar 6, 2018

I'm seeing this same issue. I'm seeing the fields listed as hidden in the object, but they aren't being removed from the actual data array.

@Siebov
Copy link

Siebov commented Nov 29, 2018

@jenssegers still not fixed?

@divine divine added bug Needs investigation Need investigation about bugs described in issue labels Feb 16, 2020
@Treggats
Copy link
Contributor

@rohitsodhia @coreywelch is this still the case? I cannot reproduce it with the following test

Test implementation
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Eloquent;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;

final class PropertyTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        Animal::truncate();
        EatableAnimal::truncate();
    }

    #[Test]
    public function can_hide_certain_properties(): void
    {
        Animal::insert([
            ['name' => 'Lion', 'country' => 'Africa', 'can_be_eaten' => false],
        ]);
        EatableAnimal::insert([
            ['name' => 'Cow', 'country' => 'Europe', 'can_be_eaten' => true],
        ]);

        [$lion] = Animal::all()->toArray();
        [$cow] = EatableAnimal::all()->toArray();
        self::assertArrayHasKey('country', $lion);
        self::assertArrayNotHasKey('country', $cow);
    }
}

/**
 * @property string $name
 * @property string $country
 * @property bool $can_be_eaten
 */
class Animal extends Eloquent
{
}

/**
 * @property string $name
 * @property string $country
 * @property bool $can_be_eaten
 */
class EatableAnimal extends Eloquent
{
    protected $hidden = ['country'];
}

@hans-thomas
Copy link
Contributor

Hi @Treggats, to close this, please open a PR and add your tests to cover this problem. much thanks.

@Treggats
Copy link
Contributor

Hi @Treggats, to close this, please open a PR and add your tests to cover this problem. much thanks.

👌 will do

@Treggats
Copy link
Contributor

@hans-thomas I've opened #2687 for this.

@hans-thomas
Copy link
Contributor

Thank you @Treggats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Needs investigation Need investigation about bugs described in issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants