Header Ads

How to create Laravel Nova search and global search

 As we know already, most websites or applications need to have a box search. to make user friendly the audience needs to search everything on our website or web application.



This video will show you detail about how to search on resources and global search on your nova resource. Global search is placed on the top of the menu bar of our Nova application. 
It performs to show the hold search result of anything from any resource, unlike resource search which is only capable of searching on resources. 

Creating search functionality in a Laravel Nova application allows users to easily find specific records based on their search queries. Laravel Nova provides built-in support for both regular search and global search, making it straightforward to implement these features. In this guide, we will walk you through the process of creating search and global search functionality in Laravel Nova.

Step 1: Prepare the Model
Before implementing search functionality, ensure that your model is set up correctly. Make sure the model has the necessary attributes and relationships defined. These attributes and relationships will be used in the search queries.

Step 2: Regular Search
To enable regular search functionality for a specific resource, you need to define the `searchable` method within the corresponding Nova resource file. Open the resource file (e.g., `app/Nova/ResourceName.php`) and add the following method:


public static function searchable()
{
    return [
        'attribute1',
        'attribute2',
        // Add more attributes as needed
    ];
}


Replace `'attribute1'`, `'attribute2'`, and so on with the actual attribute names of your model that you want to include in the search. These attributes will be used to perform the search queries.

Step 3: Implement Global Search
In addition to regular search, Laravel Nova also provides global search functionality, allowing users to perform a search across multiple resources simultaneously. To enable global search, you need to modify the `app/Nova/Search.php` file.

Open the `Search.php` file and add the following code:

use Laravel\Nova\Nova;
use Laravel\Nova\Http\Requests\NovaRequest;



public function apply(NovaRequest $request, $query, $value)
{
    $searchableResources = Nova::resourceForModel($request->model());

    foreach ($searchableResources as $resource) {
        $resource::buildIndexQuery($request, $query, $value);
    }

    return $query;
}


This code ensures that the global search query is applied to all searchable resources defined within your Laravel Nova application.

Step 4: Configure Searchable Attributes
To customize the search behavior for individual attributes, you can use the `searchableAttributes` method within your Nova resource file. Open the resource file and add the following method:


public function searchableAttributes()
{
    return [
        'attribute1',
        'attribute2',
        // Add more attributes as needed
    ];
}


Replace `'attribute1'`, `'attribute2'`, and so on with the actual attribute names you want to include in the global search. These attributes will be used to perform the global search queries.

Step 5: Customize Search Results
You can further customize the search results by overriding the `toSearchableArray` method in your model. Open your model file and add the following method:


public function toSearchableArray()
{
    return [
        'attribute1' => $this->attribute1,
        'attribute2' => $this->attribute2,
        // Add more attributes as needed
    ];
}

This method allows you to define the attributes that should be included in the search index for the model. Customize the attributes to match your specific requirements.

Step 6: Testing the Search Functionality
Once you have implemented the search functionality, you can test it in your Laravel Nova application. Start the development server by running the `serve` Artisan command:


php artisan serve


Access your Laravel Nova dashboard in a web browser (e.g., `http://localhost:8000/nova`). Perform a search in the search bar located at the top of the dashboard. The search results will be displayed based on the matching records.

Step 7: Refining the Search
If you want to refine the search behavior further, you can explore additional options offered by Laravel Scout, which is the underlying search engine used by Laravel Nova. Scout provides features like fuzzy searching, stemming, and configuring search ranking priorities. Refer to the Laravel Scout documentation for more information on these advanced search capabilities.

In conclusion, implementing search functionality in Laravel Nova involves defining searchable attributes, enabling regular search, implementing global search, configuring search attributes, and customizing search results. With these steps, you can empower users to search and locate specific records easily within your Laravel Nova application.


For the search field, we can register on 

public static search =[
    'filed_1' , ' field_2',...  /*the columm sholde be search*/
];


For the global search, we can disable or enable it by using the static method 


public static $globallySearchable = false;



No comments:

Powered by Blogger.