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

[5.5] Add Model::only method #19459

Merged
merged 2 commits into from
Jun 7, 2017
Merged

[5.5] Add Model::only method #19459

merged 2 commits into from
Jun 7, 2017

Conversation

sebastiandedeyne
Copy link
Contributor

Adds an only method to the Model class that behaves like Request::only, to quickly extract attributes from a model to an array.

$user->only('first_name', 'last_name');
// ["first_name" => "Taylor", "last_name" => "Otwell"]

$results[$attribute] = $this->getAttribute($attribute);
}

return $results;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use an array_map here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used the same style as Request::only

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tillkruss array_map won't work here because it's setting the key

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be done with array_flip or array_combine (which I prefer) though.

@sebastiandedeyne
Copy link
Contributor Author

Dot notation would be cool, but more complex since we're also dealing with accessors. I'm willing to update the PR if there's interest.

@ntzm
Copy link
Contributor

ntzm commented Jun 3, 2017

This change has been rejected before: #15748

Hopefully this does get merged in though, I find myself wanting it in new projects quite a lot!

@sebastiandedeyne
Copy link
Contributor Author

Regarding the previous PR feedback

Going to hold off on this since I feel like you could just do Arr::only($model->toArray()...

An upside of having this on the model is that you could call only on fields that are otherwise hidden.

@lucasmichot
Copy link
Contributor

lucasmichot commented Jun 5, 2017

@sebastiandedeyne,

  1. What about hidden attributes?
    With your PR, they are returned without any visibility control - which make them readable to anyone !!
$model = new EloquentModelStub;
// ...
$model->project = 'laravel';

// ...

$model->setHidden(['project']);
$this->assertEquals(['project' => 'laravel'], $model->only('project'));
// this test should have failed
  1. What about non-existing attributes?
$model = new EloquentModelStub;
// ...
$model->project = 'laravel';

// ...

$this->assertEquals(['project' => 'laravel', 'foo_bar' => null]], $model->only(['project', 'foo_bar']));
// this test should fail

$this->assertEquals(['project' => 'laravel']], $model->only(['project', 'foo_bar']));
// this test should pass

Your function should only return:

return Arr::only(
    $this->attributesToArray(),
    is_array($attributes) ? $attributes : func_get_args()
);

@calebporzio
Copy link
Contributor

@sebastiandedeyne - Needed this just yesterday! "what do you mean "->only" isn't defined???!!!"

@sebastiandedeyne
Copy link
Contributor Author

@lucasmichot I disagree on your first point. This is the perfect escape hatch for retrieving a serialiazed version of the model despite the visibility attributes.

When calling only, you're declaring which attributes you want to extract. You can't accidentally share something with the world since it's explicit.

@taylorotwell taylorotwell merged commit 51320d5 into laravel:master Jun 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants