File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
3
use Laravel \Scout \Builder ;
4
+ use Illuminate \Database \Eloquent \Collection ;
4
5
5
6
6
7
if (! Builder::hasMacro ('count ' )) {
81
82
return $ this ;
82
83
});
83
84
}
85
+
86
+ if (! Builder::hasMacro ('hydrate ' )) {
87
+ /**
88
+ * get() hydrates records by looking up the Ids in the corresponding database
89
+ * This macro uses the data returned from the search results to hydrate
90
+ * the models and return a collection
91
+ *
92
+ * @return Collection
93
+ */
94
+ Builder::macro ('hydrate ' , function () {
95
+ $ results = $ this ->engine ()->search ($ this );
96
+
97
+ if (count ($ results ['hits ' ]) === 0 ) {
98
+ return Collection::make ();
99
+ }
100
+
101
+ $ hits = collect ($ results ['hits ' ]);
102
+ $ className = get_class ($ this ->model );
103
+ $ models = new Collection ();
104
+
105
+ Eloquent::unguard ();
106
+
107
+ $ hits ->each (function ($ item , $ key ) use ($ className , $ models ) {
108
+ $ models ->push (new $ className ($ item ));
109
+ });
110
+
111
+ Eloquent::reguard ();
112
+
113
+ return $ models ;
114
+ });
115
+ }
You can’t perform that action at this time.
0 commit comments