Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fdestors's avatar

Spatie/laravel-query-builder - Search 2 columns with same search term

Hi,

I'm quite new to programming, php and Laravel. I'm currently trying to learn by developping a small personnal project. Right now, I have 3 tables :

  • Users
  • Taggables
  • Tags

The tags table are from the laravel-tags package from Spatie. I would like generate a paginated table with all my users and their associated tags. Now I would like to have a search input to search on both the user name and the tags column. I am using Spatie Laravel-Query-Builder package. So far, i've managed to list my data in my table, my filter works when i do a search on name.

But i can't figure how I can search both columns name and tags at the same time (witht he same input value). I don't even know if it is possible.

Here is my controller:


public function indexFiltering(Request $request)
{
		
        $filter = $request->query('filter');

        if (!empty($filter)) {
        
			$users = QueryBuilder::for(User::class)
                            ->allowedFields(['name','tags.name'])
                            ->with('tags')
                            ->allowedFilters('name','tags.name')
                            ->paginate(15)
                            ->appends(request()->query());
        
        } else {
            $users = User::paginate(15);
        }

        return view('users.index-sorting')->with('users', $users)->with('filter', $filter); 

    }
}

And here is my blade view:

      <div class="col-sm-12 col-md-4">
        <div>
          <form class="form-inline"  action="{{ route('users.index-filtering') }}" method="GET">
            <div class="input-group">
              <input type="text" class="form-control" id="search" name="filter[name]" placeholder="Search..." value="">
              <div class="input-group-append">
                <button class="btn btn-primary waves-effect waves-float waves-light" id="filter" type="submit">Button</button>
              </div>
            </div>
          </form>

        </div>
      </div>
    </div>

    <div class="table-responsive">
      <table class="table table-bordered mt-2">
        <thead>
            <th>Member</th>
            <th>Joined</th>
            <th>Interests</th>
            <th>Actions</th>
        </thead>
        <tbody>
            @if ($users->count() == 0)
            <tr>
                <td colspan="5">No users to display.</td>
            </tr>
            @endif
    
            @foreach ($users as $user)
            <tr>
                <td>
                  <div class="d-flex flex-row">
                      <div class="avatar me-75">
                        <a href="/users/{{ $user->slug }}">
                          <img src="{{ asset('storage') }}/{{ $user->profile_photo_path }}" alt="{{ $user->name }}" class="rounded" width="42" height="42">
                        </a>
                      </div>
                      <div class="my-auto">
                        <h6 class="mb-0"><a href="/users/{{ $user->slug }}">{{ $user->name }}</a></h6>
                        <small>{{ $user->tagline }}</small>
                      </div>
                    </div>
                </td>
                <td>{{ formatDateUs($user->created_at) }}</td>
                <td>

                  @foreach($user->tags as $tag)
                  <span class="badge bg-primary">{{ $tag->name }}</span>
                  @endforeach
                  

                </td>
                <td>{{ $user->followersCount() }}</td>
                <td>
                    <a class="btn btn-sm btn-success" href="#">Edit</a>
    
                    <form style="display:inline-block" action="#" method="POST">
                        @method('DELETE')
                        @csrf
                        <button class="btn btn-sm btn-danger"> Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach
        </tbody>
      </table>
     </div>

     <div class="mt-2">
      <?php echo $users->links(); ?>
    </div>

    <p>
        Displaying {{$users->count()}} of {{ $users->total() }} member(s).
    </p>

I read about custom filters but i don't really understand the approach with the example in the docs. Could somone please assist me on this ? Thank you

0 likes
1 reply

Please or to participate in this conversation.