Skip to content

Instantly share code, notes, and snippets.

View ArtMin96's full-sized avatar

Arthur Minasyan ArtMin96

  • Yerevan, Armenia
  • 21:48 (UTC +04:00)
View GitHub Profile
@ArtMin96
ArtMin96 / WhereLike.php
Created November 25, 2021 07:29
whereLike macro with relationship
/**
* Searching Eloquent models with Relations
*
* Example Post::whereLike(['name', 'text', 'author.name', 'tags.name'], $searchTerm)->get();
*/
Builder::macro('whereLike', function ($attributes, string $searchTerm) {
$this->where(function (Builder $query) use ($attributes, $searchTerm) {
foreach (\Arr::wrap($attributes) as $attribute) {
$query->when(
str_contains($attribute, '.'),