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

Can select only necessary columns via byHash #139

Open
addeeandra opened this issue Oct 23, 2024 · 1 comment
Open

Can select only necessary columns via byHash #139

addeeandra opened this issue Oct 23, 2024 · 1 comment

Comments

@addeeandra
Copy link

addeeandra commented Oct 23, 2024

Is your feature request related to a problem? Please describe.

Let's say that I had a table with 20 columns. The Model::byHash($hash) is getting all 20 columns by default, and hide columns from $hidden attribute. I often face a scenario where I need to get only 1 or 2 column for a table via Model::byHash($hash).

It's currently achievable with: Model::select('id', 'email')->byHash($hash)->first();. Two problems here:

  1. Loss of type-safety.
  2. Loss of conciseness, we can make it better.

Describe the solution you'd like

Capability to use Model::byHash($hash, $columns), Model::byHashOrFail($hash, $columns)

@EriBloo
Copy link
Contributor

EriBloo commented Dec 20, 2024

This could be a fine addition if signature of static byHash/byHashOrFail had a default of ['*'] columns. This would make it not break existing applications and it would be consistent with find/findOrFail.

The question I just have is what do you mean by "loss of type-safety". What is a difference in this context between Model::byHash($hash) and Model::query()->select(['*'])->byHash($hash)->first()?

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

No branches or pull requests

2 participants