Laravel 9: Scout with NEW Database Driver

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 79

  • @muhaimintaib
    @muhaimintaib 2 роки тому +14

    Managed to load relationship on my previous project using Scout with Meilisearch driver. You have to load relations inside query builder closure. Btw, Meilisearch is great if you don't want to setup the massive Elasticsearch or pay for cloud solutions (Algolia/ElasticCloud).
    $locations = Model::search($query)->query(function (Builder $builder) use $uniqueValue {
    $builder->where('field', $uniqueValue)->with('services');
    })->paginate(15);
    the $uniqueValue is an outside variable to 'fixed' filter the search throughout the same search session. (e.g: userId). No need to use it if you don't have to.

    • @donphelix5266
      @donphelix5266 Рік тому

      Dope!

    • @prasetyaabdiputra8912
      @prasetyaabdiputra8912 Рік тому

      i found an error like this Attribute `user_id` is not filterable. This index does not have configured filterable attributes. 1:8 user_id=1 can u help me to solve it

  • @chris-a-wb
    @chris-a-wb 2 роки тому +16

    At least with Algolia you can define relationships in your model
    Here's an example implementation where you're searching all of one model and then a relationship (categories)
    public function toSearchableArray()
    {
    $array = $this->toArray();
    $array = $this->transform($array);
    $array['categories'] = $this->categories->map(function ($data) {
    return $data['name'];
    })->toArray();
    return $array;
    }

  • @phoenixedge4049
    @phoenixedge4049 2 роки тому +1

    A search that doesn’t work with eloquent relationships? Complete dealbreaker for me. It’s easy enough to just write a custom search yourself. This video saved me some time, as I was about to use Scout. Now I know not to waste my time on it. Seems like others in the comments have workarounds, but it’s easier to just code the search myself.

  • @cardboarddignity
    @cardboarddignity 2 роки тому +3

    I guess Algolia or Elasticsearch (or whatever fulltext search engine) would do the trick with relations. The only downside is that you will have to perform indexing of the DB to that search engine. Also, the performance - not all servers are capable of running Docker container with Elasticsearch in it, since it's quite heavy (not sure about Algolia though). But yeah - I am considering to introduce ES to my project, since customer wants to search users by partially name and surname (like "Jo Sm" if it is John Smith) and building mega-queries in the controller (or in repository in my case) is not the best idea ever.

  • @sarkhanrasimoghlu961
    @sarkhanrasimoghlu961 2 роки тому +19

    User::search(request('search'))->query(function($query) {
    $query->with('relation');
    })->paginate(10);

    • @QuranicWorldSinghateh
      @QuranicWorldSinghateh 2 роки тому

      Yes this works well

    • @bueno20008
      @bueno20008 2 роки тому

      Funciona voy a seguir probándolo con otras formas

    • @karlebh
      @karlebh 10 місяців тому

      Works like a charm. 😅

  • @MonethiTebohoArthurnacious
    @MonethiTebohoArthurnacious 2 роки тому +4

    I have been wondering the overall purpose of Scout. I have been using local scopes for "searchable" database queries. I find that I have total control over how the query is behaves. Also works well with relationships. The controller stays the same but only one more function in the model.

    • @LaravelDaily
      @LaravelDaily  2 роки тому +4

      Yes you can do it that way. Scout was mostly built to use external tools that were built specifically for text search with a lot more features around it, not just queries, and also those external tools may run faster than your local database, ironically.

    • @muhaimintaib
      @muhaimintaib 2 роки тому

      Search and analytics databases such as Elasticsearch, Algolia, Meilisearch, Lucene, etc - are databases that were designed for the main purpose of indexing massive and mostly unstructured data.
      You can throw anything to it without having to worry about structure like SQL does, and they still do an awesome job on text search.
      Plus, having your main ACID database handling search will potentially bottleneck it at some point, e.g: Postgres will have increasing write speed as you add indexes.
      If you have/foresee small amount of data then it doesn't matter much.

  • @nabil_eisa
    @nabil_eisa 2 роки тому +1

    Haven't tried it yet but skimming through the docs, it seems you could use 'makeAllSearchableUsing()' to do your eager loading on the models , and then adding them to search in 'toSearchableArray()' but I could be wrong, see 'Modifying The Import Query' in the docs, anyways thanks for the video

  • @xnxbxs_zx
    @xnxbxs_zx 2 роки тому

    It's about time, thank you 🙌

  • @bonaventureokoli7988
    @bonaventureokoli7988 2 роки тому

    so glad about this

  • @akashsharma4846
    @akashsharma4846 Рік тому

    Hi, Thanks for this video, I know search is very useful I always use Algolia. I just want to know one thing, is it possible if user spell wrong but need to show correct result? EG: If user search 'Calofornua' can we show results to actual word 'California' ?

  • @ayuma754
    @ayuma754 Рік тому

    Hello, sir. For the index.blade, no need to add anything?

  • @firdavs.ibodullayev
    @firdavs.ibodullayev 2 роки тому

    I have a question, is it possible to add LOWER() function to index in sql query when we use scout?

  • @DarthVinsus
    @DarthVinsus 2 роки тому

    Sorry I'm a little bit late, but a possible solution for searching in relations, could it be to add on the searcheable array and index called 'country' => this->country->name or wouldn't work? maybe it will generate a N + 1 issue there right?

  • @GergelyCsermely
    @GergelyCsermely 2 роки тому

    Thanks. It looks easy but what do You think the performance?

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      The performance is the same as you would use Eloquent, nothing different.

  • @prozacsf84
    @prozacsf84 9 місяців тому

    Bro, you can just do pluck IDs on search and where in in the model alongside with eager loading

  • @sarkhanrasimoghlu961
    @sarkhanrasimoghlu961 2 роки тому +2

    Can you make a video about scout and elasticsearch? With relations search. On Algolia its easy but i want to see how you doing it in elasticsearch

    • @LaravelDaily
      @LaravelDaily  2 роки тому +2

      I don't use elasticsearch myself, so can't make a video about it.

    • @sarkhanrasimoghlu961
      @sarkhanrasimoghlu961 2 роки тому

      @@LaravelDaily than what are you using? for full text search

    • @LaravelDaily
      @LaravelDaily  2 роки тому +1

      Algolia

    • @sarkhanrasimoghlu961
      @sarkhanrasimoghlu961 2 роки тому

      @@LaravelDaily Algolia is good but i think its little bit expensive for little companies)

  • @aoporanu
    @aoporanu 2 роки тому

    Seems like a PR to search on eager loaded relationships should have no problems in getting approved?

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      There's only one way to find out :)

  • @chengkangzai
    @chengkangzai 2 роки тому +1

    rarely see Povilas using Larastater instead of QuickAdminPanel xD

    • @LaravelDaily
      @LaravelDaily  2 роки тому +2

      I use both, depending on the need, and mood :)

  • @elmarzougui
    @elmarzougui 2 роки тому +1

    I think that Laravel Scout is like spatie/laravel-searchable

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      For database driver, yes. But if you use external drivers like Algolia, Scout is much more powerful

  • @rezamortezaie5377
    @rezamortezaie5377 2 роки тому

    Firstly I'm sorry for my irrelevant question. Could you please explain what is the difference between make:channel and make:event? Are those the same things with just different directories and namespaces? Or their usages and functionality are different? Thenk you.

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      It's different. Event can be broadcasted via multiple channels.

  • @MrThito
    @MrThito 2 роки тому

    Please make a video on integrating google shopping feed api on Laravel. Really Needed that

    • @LaravelDaily
      @LaravelDaily  2 роки тому +2

      I haven't worked with it

    • @MrThito
      @MrThito 2 роки тому

      @@LaravelDaily Can we do easy multi tenancy tutorials/ guide?

    • @LaravelDaily
      @LaravelDaily  2 роки тому +1

      Search the channel, please: ua-cam.com/users/LaravelDailysearch?query=tenancy

  • @ndeblauw
    @ndeblauw 2 роки тому

    Can't you add relationship values in the searchable array definition in the model. This is kind of how algolia does it. If that works,ot sure however this would impact the performance, e.g. n±1 issues.Just thinking out loud here,didn't test anything

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      Yeah I also didn't test. Official docs don't say anything about relationships so I guess officially it's not supported

  • @enzopowell9234
    @enzopowell9234 2 роки тому

    what syntax is this *key:'search'* and *var_name:'users'* as an argument? in what version it was introduced?

    • @LaravelDaily
      @LaravelDaily  2 роки тому +1

      PHP 8 named parameters. Also, phpstorm auto suggests this to me

    • @enzopowell9234
      @enzopowell9234 2 роки тому

      @@LaravelDaily Thank you for the reply. Could you please talk about DTO in Laravel?

  • @HadayatNiazi-xp1fz
    @HadayatNiazi-xp1fz Рік тому

    Thank you very much sir

  • @pahtashow
    @pahtashow 2 роки тому

    There is support for morphologies in Russian, English etc ?

  • @iyolimlimgo8822
    @iyolimlimgo8822 2 роки тому

    what is the code in the search component?

  • @gazorbpazorbian
    @gazorbpazorbian 2 роки тому

    what about if you want to look for username and users that are active(as a boolean field)?

    • @LaravelDaily
      @LaravelDaily  2 роки тому +1

      You add that as a where stateme probably

    • @bossmac1122
      @bossmac1122 2 роки тому +1

      maybe something like this?
      public function shouldBeSearchable(): bool
      {
      return $this->is_active;
      }

  • @muhammadfahad4826
    @muhammadfahad4826 2 роки тому

    Can you kindly suggest me a complete working tutorial to implement elastic search in laravel 9?

    • @LaravelDaily
      @LaravelDaily  2 роки тому +1

      I don't have anything about it myself, sorry, google it

    • @mohammadfahad336
      @mohammadfahad336 2 роки тому

      @@LaravelDaily thanks anyways
      If anyone else looking for something try doing it with elasticsearch package

  • @version_dew
    @version_dew 2 роки тому

    Not related to the topic but how to fix that flickering when you click to another page or refresh the page? I tried adding x-cloak but its not working.

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      X-cloak should be the solution, not sure why it's not working for you. It should be x-cloak and then adding css style for it.

    • @version_dew
      @version_dew 2 роки тому

      @@LaravelDaily I tried it on newly installed larastarter with tailwind components, it did fix the dropdown for profile and logout but I don't know where to put it to fix the whole page, I tried putting it at the app and individual components but it didn't work.

  • @dhruvangdave3695
    @dhruvangdave3695 Рік тому +1

    I came here just to see how scout works with relationships... 🤕

  • @smith4591
    @smith4591 2 роки тому

    Is it possible to use this in Laravel 8 too?

  • @reynanresurreccion8313
    @reynanresurreccion8313 2 роки тому

    in the next video search in relation table.

  • @phuctranhoang8208
    @phuctranhoang8208 Рік тому

    you are using Algolia not Elastics, right? I don't see anything relative in elastics in your code of laravel

    • @LaravelDaily
      @LaravelDaily  Рік тому

      It's the database driver, internal Laravel with DB locally, no algolia or elastic

  • @sawadogosidiki8926
    @sawadogosidiki8926 2 роки тому

    Merci beaucoup interessant mais je ne comprend pas l'anglais. Songez a traduire le son de vos tutoriel en francais aussi

  • @SalomDunyoIT
    @SalomDunyoIT 2 роки тому +1

    I use my own simple local scope:
    on top you need to have search property in this format: $search = [ 'name', 'country' => [ 'name' ] ]; it works pretty well for me.
    public function scopeSearch($query, $string = NULL)
    {
    if ($string == null) return $query;
    $words = [];
    if (stripos($string, ' ') !== false) {
    $words = explode(' ', $string);
    }
    $q = $query;
    if (count($words) > 0) {
    foreach ($words as $item) {
    foreach ($this->search as $key => $column) {
    if (is_array($column) && count($column) > 0) {
    $q->orWhereHas($key, function ($query) use ($column, $item) {
    $query->where(function ($sub_query) use ($column, $item) {
    foreach ($column as $sub_column) {
    $sub_query->orWhere($sub_column, 'LIKE', "%{$item}%");
    }
    });
    });
    } else $q->orWhere($column, 'LIKE', "%{$item}%");
    }
    }
    return $q;
    } else {
    $q->where(function ($qq) use ($string) {
    foreach ($this->search as $key => $column) {
    if (is_array($column) && count($column) > 0) {
    $qq->orWhereHas($key, function ($query) use ($column, $string) {
    $query->where(function ($sub_query) use ($column, $string) {
    foreach ($column as $sub_column) {
    $sub_query->orWhere($sub_column, 'LIKE', "%{$string}%");
    }
    });
    });
    } else $qq->orWhere($column, 'LIKE', "%{$string}%");
    }
    });
    return $q;
    }
    }

  • @thisisthelogic
    @thisisthelogic 2 роки тому

    Are you in Lithuania? Get out of there!

    • @LaravelDaily
      @LaravelDaily  2 роки тому

      Yes it's getting "hotter" in here, but ok for now. Thanks for the advice!

  • @bdsumon4u
    @bdsumon4u 2 роки тому +2

    User::search('term')->query(fn ($query) => $query->with('country'))->paginate(10);

    • @bdsumon4u
      @bdsumon4u 2 роки тому

      I'd used that in one of my previous projects. I don’t know whether it works now or not.
      If it works, thank me in your next video. Else if doesn’t work, then I'm sorry.

    • @10ccepi
      @10ccepi 2 роки тому

      @@bdsumon4u Doesn't seem to.

    • @reynanresurreccion8313
      @reynanresurreccion8313 2 роки тому

      this code is not working

  • @svenst
    @svenst 2 роки тому

    Why not simply using MySQL fulltext search?

    • @fahnleindieselschweif5022
      @fahnleindieselschweif5022 2 роки тому

      Did you read the docs? (#[SearchUsingFullText(['bio'])])

    • @svenst
      @svenst 2 роки тому

      @@fahnleindieselschweif5022 my fault. thanks.

  • @fahnleindieselschweif5022
    @fahnleindieselschweif5022 2 роки тому

    I am using teamtnt/laravel-scout-tntsearch-driver but I would really like to use a standard laravel function.

    • @lucasj.pereira4912
      @lucasj.pereira4912 2 роки тому

      I like this package because it returns some results even when the user makes some typo, like, searching for 'fod' will also return 'food' from tnt. This is not possible using the "like" in mysql