This Laravel package allows you to search through multiple Eloquent models. It supports sorting, pagination, scoped queries, eager load relationships, and searching through single or multiple columns.
Hey! We've built a Docker-based deployment tool to launch apps and sites fully containerized. You can find all features and the roadmap on our website, and we are on Twitter as well!
If you want to know more about this package's background, please read the blog post.
We proudly support the community by developing Laravel packages and giving them away for free. Keeping track of issues and pull requests takes time, but we're happy to help! If this package saves you time or if you're relying on it professionally, please consider supporting the maintenance and development.
You can install the package via composer:
composer require protonemedia/laravel-cross-eloquent-search
startWithWildcard
method has been renamed to beginWithWildcard
.getUpdatedAtColumn
method. Previously it was hard-coded to updated_at
. You still can use another column to order by.allowEmptySearchQuery
method and EmptySearchQueryException
class have been removed, but you can still get results without searching.Start your search query by adding one or more models to search through. Call the add
method with the model's class name and the column you want to search through. Then call the get
method with the search term, and you'll get a \Illuminate\Database\Eloquent\Collection
instance with the results.
The results are sorted in ascending order by the updated column by default. In most cases, this column is updated_at
. If you've customized your model's UPDATED_AT
constant, or overwritten the getUpdatedAtColumn
method, this package will use the customized column. Of course, you can order by another column as well.
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
$results = Search::add(Post::class, 'title')
->add(Video::class, 'title')
->get('howto');
If you care about indentation, you can optionally use the new
method on the facade:
Search::new()
->add(Post::class, 'title')
->add(Video::class, 'title')
->get('howto');
You can add multiple models at once by using the addMany
method:
Search::addMany([
[Post::class, 'title'],
[Video::class, 'title'],
])->get('howto');
There's also an addWhen
method, that adds the model when the first argument given to the method evaluates to true
:
Search::new()
->addWhen($user, Post::class, 'title')
->addWhen($user->isAdmin(), Video::class, 'title')
->get('howto');
By default, we split up the search term, and each keyword will get a wildcard symbol to do partial matching. Practically this means the search term apple ios
will result in apple%
and ios%
. If you want a wildcard symbol to begin with as well, you can call the beginWithWildcard
method. This will result in %apple%
and %ios%
.
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->beginWithWildcard()
->get('os');
Note: in previous versions of this package, this method was called startWithWildcard()
.
If you want to disable the behaviour where a wildcard is appended to the terms, you should call the endWithWildcard
method with false
:
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->beginWithWildcard()
->endWithWildcard(false)
->get('os');
Multi-word search is supported out of the box. Simply wrap your phrase into double-quotes.
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->get('"macos big sur"');
You can disable the parsing of the search term by calling the dontParseTerm
method, which gives you the same results as using double-quotes.
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->dontParseTerm()
->get('macos big sur');
If you want to sort the results by another column, you can pass that column to the add
method as a third parameter. Call the orderByDesc
method to sort the results in descending order.
Search::add(Post::class, 'title', 'published_at')
->add(Video::class, 'title', 'released_at')
->orderByDesc()
->get('learn');
You can call the orderByRelevance
method to sort the results by the number of occurrences of the search terms. Imagine these two sentences:
If you search for Apple iPad, the second sentence will come up first, as there are more matches of the search terms.
Search::add(Post::class, 'title')
->beginWithWildcard()
->orderByRelevance()
->get('Apple iPad');
Ordering by relevance is not supported if you're searching through (nested) relationships.
To sort the results by model type, you can use the orderByModel
method by giving it your preferred order of the models:
Search::new()
->add(Comment::class, ['body'])
->add(Post::class, ['title'])
->add(Video::class, ['title', 'description'])
->orderByModel([
Post::class, Video::class, Comment::class,
])
->get('Artisan School');
We highly recommend paginating your results. Call the paginate
method before the get
method, and you'll get an instance of \Illuminate\Contracts\Pagination\LengthAwarePaginator
as a result. The paginate
method takes three (optional) parameters to customize the paginator. These arguments are the same as Laravel's database paginator.
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->paginate()
// or
->paginate($perPage = 15, $pageName = 'page', $page = 1)
->get('build');
You may also use simple pagination. This will return an instance of \Illuminate\Contracts\Pagination\Paginator
, which is not length aware:
Search::add(Post::class, 'title')
->add(Video::class, 'title')
->simplePaginate()
// or
->simplePaginate($perPage = 15, $pageName = 'page', $page = 1)
->get('build');
Instead of the class name, you can also pass an instance of the Eloquent query builder to the add
method. This allows you to add constraints to each model.
Search::add(Post::published(), 'title')
->add(Video::where('views', '>', 2500), 'title')
->get('compile');
You can search through multiple columns by passing an array of columns as the second argument.
Search::add(Post::class, ['title', 'body'])
->add(Video::class, ['title', 'subtitle'])
->get('eloquent');
You can search through (nested) relationships by using the dot notation:
Search::add(Post::class, ['comments.body'])
->add(Video::class, ['posts.user.biography'])
->get('solution');
MySQL has a soundex algorithm built-in so you can search for terms that sound almost the same. You can use this feature by calling the soundsLike
method:
Search::new()
->add(Post::class, 'framework')
->add(Video::class, 'framework')
->soundsLike()
->get('larafel');
Not much to explain here, but this is supported as well :)
Search::add(Post::with('comments'), 'title')
->add(Video::with('likes'), 'title')
->get('guitar');
You call the get
method without a term or with an empty term. In this case, you can discard the second argument of the add
method. With the orderBy
method, you can set the column to sort by (previously the third argument):
Search::add(Post::class)
->orderBy('published_at')
->add(Video::class)
->orderBy('released_at')
->get();
You can count the number of results with the count
method:
Search::add(Post::published(), 'title')
->add(Video::where('views', '>', 2500), 'title')
->count('compile');
You can use the parser with the parseTerms
method:
$terms = Search::parseTerms('drums guitar');
You can also pass in a callback as a second argument to loop through each term:
Search::parseTerms('drums guitar', function($term, $key) {
//
});
composer test
Please see CHANGELOG for more information about what has changed recently.
Please see CONTRIBUTING for details.
Laravel Analytics Event Tracking
: Laravel package to easily send events to Google Analytics.Laravel Blade On Demand
: Laravel package to compile Blade templates in memory.Laravel Eloquent Scope as Select
: Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.Laravel Eloquent Where Not
: This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.Laravel FFMpeg
: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.Laravel Form Components
: Blade components to rapidly build forms with Tailwind CSS Custom Forms and Bootstrap 4. Supports validation, model binding, default values, translations, includes default vendor styling and fully customizable!Laravel Mixins
: A collection of Laravel goodies.Laravel Paddle
: Paddle.com API integration for Laravel with support for webhooks/events.Laravel Verify New Email
: This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.Laravel WebDAV
: WebDAV driver for Laravel's Filesystem.If you discover any security-related issues, please email pascal@protone.media instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package is Treeware. If you use it in production, we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest, you'll create employment for local families and restoring wildlife habitats.
集合 collect 会为指定的数组返回一个新的实例,创建一个集合 $collection = collect([1,2,3]); 扩展集合 集合都是 宏观的,他允许您在执行时将其他方法添加到 collection类。例如,通过下面的代码在collection类中 添加一个tuUpper方法; use Illuminate\Support\Str Collection:macro('toUpper
集合 [TOC] 简介 Illuminate\Support\Collection 类提供了一个更具可读性和更便于处理数组数据的封装。具体请查看下方示例代码。我们使用 collect辅助函数从数组中创建一个新的集合实例,对其中每一个元素执行strtoupper 函数之后再删除所有的空元素: $collection = collect(['taylor', 'abigail', null])->ma
原文链接 必备品 文档:Documentation API:API Reference 视频:Laracasts 新闻:Laravel News 中文文档 Laravel学院– Laravel 5.1 中文文档 Laravel中文网– 由PHPHub站长驱动 Laravel中文网– 由Bootstrap中文网站长驱动 Laravel台湾– Laravel文档繁体中文版 Laravel 5 基于2
关于 拾年之璐 微信公众号:知行校园汇,点击查看,欢迎关注 其他平台(点击蓝字可访问): GitHub | Gitee | 哔哩哔哩 | 语雀 | 简书 | 微信小程序 | 知行达摩院 本文专栏:Laravel 点击查看系列文章 9.1 创建与使用集合 什么是集合? 它是一种更具读取性和处理能力的数组封装。 比如,我们从数据库得到的数据列表,它就是一种集合;我们可以对
最近一直在用laravel框架,比较喜欢laravel的ORM(通常我们理解的Model)...但是默认情况下,Eloquent 查询的结果总是返回 Collection 实例...所有不得不了解collection~~~ 一点点自己的理解,如有错误,还请不吝赐教!!!定然十分感谢! 创建集合 默认我们model查出来的就是集合,创建也很简单:辅助函数 collect 为给定数组返回一个新的
简介 Illuminate\Support\Collection 类为处理数组数据提供了流式、方便的封装。例如,查看下面的代码,我们使用辅助函数 collect 创建一个新的集合实例,为每一个元素运行 strtoupper 函数,然后移除所有空元素: $collection = collect(['taylor', 'abigail', null])->map(function ($name) {
问题内容: 我该如何总结一个渴望加载的数据集? 这是我的表结构: 这些是我的模型/关系: 这是我的控制器 这是返回的数据的样子(以json格式,我理解雄辩的集合中的$ regions):- 我不知道如何将“交易”(4)的总和发送回视图? 问题答案: $deals = $regions->sum(function ($region) { return $region->submits->sum(‘d
Laravel Eloquent Query Cache Laravel Eloquent Query Cache brings back the remember() functionality that has been removed from Laravel a long time ago.It adds caching functionalities directly on the El
我通常会用能言善辩的拉雷维尔分别选择我的物品 我得到的是一个照明\数据库\雄辩\收藏与一个项目。后来我把它们放在一个数组中,这样我就有了这些照明\数据库\雄辩\集合对象的数组。 然而,有时我需要更多,所以我会这样做: 这是一个包含多个项目的集合。有没有一个简单的方法来改变一个照明\数据库\雄辩\集合的数组中的几个项目与单项照明\数据库\雄辩\集合? 我当然能做到: 但再次从DB中选择似乎是一个相当
问题内容: Laravel 4的口才ORM 和之间有什么区别?我尝试环顾四周,但找不到任何东西! 问题答案: 连接(): 处理多对多关系时插入相关模型 预期没有数组参数 例: 同步(): 与该方法类似,该方法用于附加相关模型。但是,主要区别在于: 接受一组ID放置在数据透视表上 其次, 最重要的是 ,如果数组中不存在模型,则sync方法将从表中删除模型,并将仅新项插入到数据透视表中。 例: use
有两个表 表 user id name 1 tom 2 jerry 表 friend id user_id support_id 1 1 1 2 1 2 表 support id name can_fly 1 bird 1 2 dog 0 三个 Model 我想实现如下 sql,意义为 获取 tom 所有会飞的朋友 关系 我这样写,但后边的不会写了,求解。
问题内容: 对于 Maria-DB* 和 MySQL 支持的 动态 列,我们具有JSON列类型。对于我们的项目之一,我们应该为 Maria- DB (不是 Mysql )实现一个数据库。 *** 使用包支持“ 动态列”。 如何可以覆盖orm中进行添加。在将此功能添加到此类中的包中,可以覆盖该类 框架中支持ORM的实现类: DynamicActiveRecord.php DynamicActiveQ