Skip to content

Commit 5400397

Browse files
Make eloquent builder return an eloquent Collection
1 parent 6d4a4fd commit 5400397

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/Database/Eloquent/FMEloquentBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use GearboxSolutions\EloquentFileMaker\Exceptions\FileMakerDataApiException;
88
use Illuminate\Contracts\Support\Arrayable;
99
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Collection;
1011
use Illuminate\Database\Eloquent\Scope;
1112
use Illuminate\Pagination\Paginator;
1213
use Illuminate\Support\Arr;
13-
use Illuminate\Support\Collection;
1414

1515
class FMEloquentBuilder extends Builder
1616
{
@@ -265,8 +265,8 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
265265
/**
266266
* Compares a model's modified portal data and original portal data and returns portal data with only modified fields and recordIds
267267
*
268-
* @param $array1 array The modified portal data
269-
* @param $array2 array The model's original portal data
268+
* @param $array1 array The modified portal data
269+
* @param $array2 array The model's original portal data
270270
*/
271271
protected function getOnlyModifiedPortalFields($array1, $array2): array
272272
{

src/Database/Eloquent/FMModel.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
use GearboxSolutions\EloquentFileMaker\Database\Query\FMBaseBuilder;
99
use GearboxSolutions\EloquentFileMaker\Exceptions\FileMakerDataApiException;
1010
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Database\Eloquent\Collection;
1112
use Illuminate\Database\Eloquent\Model;
1213
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
1314
use Illuminate\Database\Eloquent\Relations\Pivot;
1415
use Illuminate\Http\File;
1516
use Illuminate\Http\UploadedFile;
16-
use Illuminate\Support\Collection;
17+
use Illuminate\Support\Collection as BaseCollection;
1718
use Illuminate\Support\Str;
1819

1920
abstract class FMModel extends Model
@@ -145,24 +146,19 @@ public static function createFromRecord($record)
145146
return $instance;
146147
}
147148

148-
public static function createModelsFromRecordSet(Collection $records): Collection
149+
public static function createModelsFromRecordSet(BaseCollection $records): Collection
149150
{
150-
151-
// start with an empty collection
152-
$collection = collect([]);
153-
154151
// return an empty collection if an empty collection was passed in.
155152
if ($records->count() === 0) {
156-
return $collection;
153+
return (new static())->newCollection([]);
157154
}
158155

159156
// Records passed in weren't empty, so process the records
160-
foreach ($records as $record) {
161-
$model = static::createFromRecord($record);
162-
$collection->push($model);
163-
}
157+
$mappedRecords = $records->map(function ($record) {
158+
return static::createFromRecord($record);
159+
});
164160

165-
return $collection;
161+
return (new static())->newCollection($mappedRecords->all());
166162
}
167163

168164
/** Fill in data for this existing model with record data from FileMaker
@@ -396,7 +392,7 @@ protected function performInsert(Builder $query)
396392
/**
397393
* Strip out containers and read-only fields to prepare for a write query
398394
*
399-
* @return Collection
395+
* @return BaseCollection
400396
*/
401397
public function getAttributesForFileMakerWrite()
402398
{

0 commit comments

Comments
 (0)