You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An idea that I believe would greatly enhance the functionality of Eloquent Builder, specifically concerning search queries utilizing the "whereLike" functionality.
Currently, in my projects, I've been utilizing a custom scope named "whereLike" within my models to facilitate search operations based on partial matches across multiple columns. Here's a snippet of the implementation I've been using:
public function scopeWhereLike($query, $columns, $q){
return $query->orwhere(function ($query) use ($columns, $q) {
foreach ($columns as $column) {
$query->orWhere($column, 'like', '%'.$q.'%');
}
});
}
public function scopeWhereHasLike($query, $relation, $columns, $q){
return $query->orwhereHas($relation, function ($query) use ($columns, $q) {
foreach ($columns as $column) {
$query->Where($columns[0], 'like', '%'.$q.'%')->orwhere($column, 'like', '%'.$q.'%');
}
});
}
This approach has been effective, but I envision an even more streamlined and expressive solution by integrating a native "whereLike" syntax directly into the Eloquent Builder.
The proposed syntax could potentially resemble the following:
return Receiving::with("fk_vendor_id")
->whereLike(["invoice_no"], $request->q) // custom scope for like
->WhereHasLike("fk_vendor_id", ["name", "primary_phone"], $request->q) // custom scope for relational like
->leftJoin('vendors', 'receivings.fk_vendor_id', '=', 'vendors.id')
->selectRaw("receivings.*, $request->column as text_column, CONCAT(invoice_no, ' : ', vendors.name, ' : ', vendors.primary_phone) as text")
->groupBy("$request->column")
->get()
->take(10);
By incorporating this feature directly into Eloquent Builder, it would not only simplify the codebase but also enhance readability and maintainability across projects. This native support for "whereLike" queries would undoubtedly streamline the development process for search functionality, making it more intuitive for developers to implement and maintain.
I believe this addition would be immensely beneficial to the Laravel community, as it aligns with the framework's ethos of promoting elegant and efficient solutions for common development tasks.
I am excited about the potential of this feature and would greatly appreciate your consideration in integrating it into future releases of Laravel.
This discussion was converted from issue #50331 on March 03, 2024 07:33.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
10.46.0
PHP Version
8.2.12
Database Driver & Version
MySQL 8
Description
An idea that I believe would greatly enhance the functionality of Eloquent Builder, specifically concerning search queries utilizing the "whereLike" functionality.
Currently, in my projects, I've been utilizing a custom scope named "whereLike" within my models to facilitate search operations based on partial matches across multiple columns. Here's a snippet of the implementation I've been using:
This approach has been effective, but I envision an even more streamlined and expressive solution by integrating a native "whereLike" syntax directly into the Eloquent Builder.
The proposed syntax could potentially resemble the following:
By incorporating this feature directly into Eloquent Builder, it would not only simplify the codebase but also enhance readability and maintainability across projects. This native support for "whereLike" queries would undoubtedly streamline the development process for search functionality, making it more intuitive for developers to implement and maintain.
I believe this addition would be immensely beneficial to the Laravel community, as it aligns with the framework's ethos of promoting elegant and efficient solutions for common development tasks.
I am excited about the potential of this feature and would greatly appreciate your consideration in integrating it into future releases of Laravel.
Discussion happens on #50288
Beta Was this translation helpful? Give feedback.
All reactions