GithubHelp home page GithubHelp logo

Comments (2)

wlius-support3 avatar wlius-support3 commented on July 20, 2024

I think the best workaround is to create a MySQL View.
Brent from Spatie has a good article on it.

You can then reference the "MySQL View" model (which is still a regular eloquent model, it just points to a view instead of a table), and now you can search across relationships because the view can be built to join the relationships.

from laravel-scout-mysql-driver.

sawtell avatar sawtell commented on July 20, 2024

Thanks @wlius-support3 for the idea to use a View.

This may help others:
I needed to perform the search on the "main" model (and return it as the result) but obviously use the View model for the actual search query.

I overrode the search() method on the main model so that the query is performed on the search View.

MainModel.php

public static function search($query = '', $callback = null) {
    return app(Builder::class, [
        'model' => new MainModelSeachView(), // Just this changed from the parent implementation.
        'query' => $query,
        'callback' => $callback,
        'softDelete'=> static::usesSoftDelete() && config('scout.soft_delete', false),
    ]);
}

Then in the View model I overrode newFromBuilder() in order to return the result as the main model.

MainModelSearchView.php

public function newFromBuilder($attributes = [], $connection = NULL) {
    return MainModel::find($attributes->id); // Ensure the ID is available in the View table
}

This way I can perform a search like MainModel::search('query') but the View is queried and I still get the result as a collection of MainModels.

Would love to know if there's a better way to do this.

from laravel-scout-mysql-driver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.