Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Search Relations

titasgailius/search-relations image

Search Relations stats

Downloads
4.5M
Stars
330
Open Issues
6
Forks
33

View on GitHub →

A Laravel Nova tool.

I stand with Ukraine πŸ‡ΊπŸ‡¦

Search relationships in Laravel Nova

This package allows you to include relationship columns into Laravel Nova search query.

Screenshot

Installation

composer require titasgailius/search-relations

Next, add Titasgailius\SearchRelations\SearchesRelations trait to your base resource class App\Nova\Resource

use Titasgailius\SearchRelations\SearchesRelations;
 
abstract class Resource extends NovaResource
{
use SearchesRelations;

Usage

Simply add public static $searchRelations variable to any of your Nova resources. This array accepts a relationship name as a key and an array of searchable columns as a value.

/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user' => ['username', 'email'],
];

Alternatively, you may add a static searchableRelations() method to return an array of searchable relations.

/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableRelations(): array
{
return [
'user' => ['username', 'email'],
];
}

Global search

You may customize the rules of your searchable relationships for global search by defining the $globalSearchRelations property.

/**
* The relationship columns that should be searched globally.
*
* @var array
*/
public static $globalSearchRelations = [
'user' => ['email'],
];

Alternatively, you may add a static globallySearchableRelations() method to return an array of globally searchable relations.

/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function globallySearchableRelations(): array
{
return [
'user' => ['email'],
];
}

Disabling global search for relationships

You may disable the global relationship search by declaring $globalSearchRelations with an empty array.

/**
* The relationship columns that should be searched globally.
*
* @var array
*/
public static $globalSearchRelations = [];

Alternatevily, you may disable the global search for relationships by setting the $searchRelationsGlobally property to false.

/**
* Determine if relations should be searched globally.
*
* @var array
*/
public static $searchRelationsGlobally = false;

Nested relationships

You may search nested relationships using dot notation.

/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user.country' => ['code'],
];

Extending Search

You may apply custom search logic for the specified relations by retuning a class implementing a Search interface.

/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableRelations(): array
{
return [
'country' => new LocationSearch(['USA', 'UK']),
];
}

Your custom search class must implement a simple Search interface that has a single method which accepts the current query $query, a relationship name $relation and a search input $search.

<?php
 
namespace Titasgailius\SearchRelations\Contracts;
 
use Illuminate\Database\Eloquent\Builder;
 
interface Search
{
/**
* Apply search for the given relation.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $relation
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
public function apply(Builder $query, string $relation, string $search): Builder;
}

You may take a look at the Titasgailius\SearchRelations\Searches\RelationSearch class as an example.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Titasgailius Search Relations Related Articles

Laravel 13 Released: PHP 8.3, Attributes, Laravel AI, and a Smoother Upgrade Path image

Laravel 13 Released: PHP 8.3, Attributes, Laravel AI, and a Smoother Upgrade Path

Read article
Laravel Related Content: Semantic Relationships Using pgvector image

Laravel Related Content: Semantic Relationships Using pgvector

Read article
Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension image

Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension

Read article
What's new in Filament v4.5? image

What's new in Filament v4.5?

Read article
The Hidden Cost of MySQL Defaults in Laravel Apps image

The Hidden Cost of MySQL Defaults in Laravel Apps

Read article
Powerful Debugging for PHP & Laravel with LaraDumps image

Powerful Debugging for PHP & Laravel with LaraDumps

Read article
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech