New Eloquent Constraint: Model::whenWhere('column', $value) #43767
Unanswered
achmedislamic
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have Livewire component that contain table. That table have search functionality. For that to work, I usually use it like this:
Model::when($this->status, fn ($query) => $query->where('status_id', $this->status))->when($this->jurusan, fn ($query) => $query->where('jurusan_id', $this->jurusan))->paginate()
Because search column is optional, I'm using when constraint. I have an idea to create new constraint called whenWhere. That code above will be change like below:
Model::whenWhere('status_id', $this->status)->whenWhere('jurusan_id', $this->jurusan)->paginate()
This is a lot cleaner and more understandable in my opinion. Right now, I'm using scope to achieve that and would be cool if this feature can be added to Laravel.
Let me know what you think guys.
// I'm sorry, english isn't my native language. I hope you understand with what I say.
Beta Was this translation helpful? Give feedback.
All reactions