Skip to content

Commit 53bce2e

Browse files
authored
Add Laravel 12 quiet methods and createOrFirst to relations (#651)
1 parent 5b2f43b commit 53bce2e

File tree

5 files changed

+389
-0
lines changed

5 files changed

+389
-0
lines changed

src/Database/Concerns/HasNicerPagination.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ public function simplePaginateCustom($perPage, $pageName)
5252
{
5353
return $this->simplePaginate($perPage, ['*'], $pageName);
5454
}
55+
56+
/**
57+
* cursorPaginateAtPage paginates using a cursor by passing the cursor directly.
58+
*
59+
* @param int $perPage
60+
* @param \Illuminate\Pagination\Cursor|string|null $cursor
61+
* @return \Illuminate\Contracts\Pagination\CursorPaginator
62+
*/
63+
public function cursorPaginateAtPage($perPage, $cursor)
64+
{
65+
return $this->cursorPaginate($perPage, ['*'], 'cursor', $cursor);
66+
}
67+
68+
/**
69+
* cursorPaginateCustom paginates using a cursor with a custom cursor name.
70+
*
71+
* @param int $perPage
72+
* @param string $cursorName
73+
* @return \Illuminate\Contracts\Pagination\CursorPaginator
74+
*/
75+
public function cursorPaginateCustom($perPage, $cursorName)
76+
{
77+
return $this->cursorPaginate($perPage, ['*'], $cursorName);
78+
}
5579
}

src/Database/Relations/AttachOneOrMany.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,38 @@ public function save(Model $model, $sessionKey = null)
138138
return $model->save() ? $model : false;
139139
}
140140

141+
/**
142+
* saveQuietly saves the supplied related model without raising any events.
143+
*/
144+
public function saveQuietly(Model $model, $sessionKey = null)
145+
{
146+
return Model::withoutEvents(function () use ($model, $sessionKey) {
147+
return $this->save($model, $sessionKey);
148+
});
149+
}
150+
151+
/**
152+
* saveMany saves multiple models with deferred binding support.
153+
*/
154+
public function saveMany($models, $sessionKey = null)
155+
{
156+
foreach ($models as $model) {
157+
$this->save($model, $sessionKey);
158+
}
159+
160+
return $models;
161+
}
162+
163+
/**
164+
* saveManyQuietly saves multiple models without raising any events.
165+
*/
166+
public function saveManyQuietly($models, $sessionKey = null)
167+
{
168+
return Model::withoutEvents(function () use ($models, $sessionKey) {
169+
return $this->saveMany($models, $sessionKey);
170+
});
171+
}
172+
141173
/**
142174
* create a new instance of this related model
143175
*/
@@ -162,6 +194,40 @@ public function create(array $attributes = [], $sessionKey = null)
162194
return $model;
163195
}
164196

197+
/**
198+
* createQuietly creates a new instance without raising any events.
199+
*/
200+
public function createQuietly(array $attributes = [], $sessionKey = null)
201+
{
202+
return Model::withoutEvents(function () use ($attributes, $sessionKey) {
203+
return $this->create($attributes, $sessionKey);
204+
});
205+
}
206+
207+
/**
208+
* createMany creates multiple instances of related models.
209+
*/
210+
public function createMany(iterable $records, $sessionKey = null)
211+
{
212+
$instances = [];
213+
214+
foreach ($records as $record) {
215+
$instances[] = $this->create($record, $sessionKey);
216+
}
217+
218+
return $instances;
219+
}
220+
221+
/**
222+
* createManyQuietly creates multiple instances without raising any events.
223+
*/
224+
public function createManyQuietly(iterable $records, $sessionKey = null)
225+
{
226+
return Model::withoutEvents(function () use ($records, $sessionKey) {
227+
return $this->createMany($records, $sessionKey);
228+
});
229+
}
230+
165231
/**
166232
* createFromFile
167233
*/

src/Database/Relations/BelongsToMany.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,40 @@ public function save(Model $model, array $pivotData = [], $sessionKey = null)
6868
return $model;
6969
}
7070

71+
/**
72+
* saveQuietly saves the model without raising any events,
73+
* with deferred binding support.
74+
*/
75+
public function saveQuietly(Model $model, array $pivotData = [], $sessionKey = null)
76+
{
77+
return Model::withoutEvents(function () use ($model, $pivotData, $sessionKey) {
78+
return $this->save($model, $pivotData, $sessionKey);
79+
});
80+
}
81+
82+
/**
83+
* saveMany saves multiple models with deferred binding support.
84+
*/
85+
public function saveMany($models, array $pivotData = [], $sessionKey = null)
86+
{
87+
foreach ($models as $model) {
88+
$this->save($model, $pivotData, $sessionKey);
89+
}
90+
91+
return $models;
92+
}
93+
94+
/**
95+
* saveManyQuietly saves multiple models without raising any events,
96+
* with deferred binding support.
97+
*/
98+
public function saveManyQuietly($models, array $pivotData = [], $sessionKey = null)
99+
{
100+
return Model::withoutEvents(function () use ($models, $pivotData, $sessionKey) {
101+
return $this->saveMany($models, $pivotData, $sessionKey);
102+
});
103+
}
104+
71105
/**
72106
* create a new instance of this related model with deferred binding support.
73107
*/
@@ -80,6 +114,55 @@ public function create(array $attributes = [], array $pivotData = [], $sessionKe
80114
return $model;
81115
}
82116

117+
/**
118+
* createQuietly creates a new instance without raising any events,
119+
* with deferred binding support.
120+
*/
121+
public function createQuietly(array $attributes = [], array $pivotData = [], $sessionKey = null)
122+
{
123+
return Model::withoutEvents(function () use ($attributes, $pivotData, $sessionKey) {
124+
return $this->create($attributes, $pivotData, $sessionKey);
125+
});
126+
}
127+
128+
/**
129+
* createMany creates multiple related models with deferred binding support.
130+
*/
131+
public function createMany(iterable $records, array $pivotData = [], $sessionKey = null)
132+
{
133+
$instances = $this->related->newCollection();
134+
135+
foreach ($records as $record) {
136+
$instances->push($this->create($record, $pivotData, $sessionKey));
137+
}
138+
139+
return $instances;
140+
}
141+
142+
/**
143+
* createManyQuietly creates multiple models without raising any events,
144+
* with deferred binding support.
145+
*/
146+
public function createManyQuietly(iterable $records, array $pivotData = [], $sessionKey = null)
147+
{
148+
return Model::withoutEvents(function () use ($records, $pivotData, $sessionKey) {
149+
return $this->createMany($records, $pivotData, $sessionKey);
150+
});
151+
}
152+
153+
/**
154+
* createOrFirst attempts to create the record, or if a unique constraint
155+
* violation occurs, finds the existing record.
156+
*/
157+
public function createOrFirst(array $attributes = [], array $values = [], array $pivotData = [], $sessionKey = null)
158+
{
159+
$model = $this->related->createOrFirst($attributes, $values);
160+
161+
$this->add($model, $sessionKey, $pivotData);
162+
163+
return $model;
164+
}
165+
83166
/**
84167
* attach overrides attach() method of BelongToMany relation
85168
* This is necessary in order to fire 'model.relation.beforeAttach', 'model.relation.attach' events
@@ -292,6 +375,26 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
292375
return $paginator;
293376
}
294377

378+
/**
379+
* cursorPaginate using a cursor paginator.
380+
*
381+
* @param int|null $perPage
382+
* @param array $columns
383+
* @param string $cursorName
384+
* @param \Illuminate\Pagination\Cursor|string|null $cursor
385+
* @return \Illuminate\Contracts\Pagination\CursorPaginator
386+
*/
387+
public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
388+
{
389+
$this->query->addSelect($this->shouldSelect($columns));
390+
391+
$paginator = $this->query->cursorPaginate($perPage, $columns, $cursorName, $cursor);
392+
393+
$this->hydratePivotRelation($paginator->items());
394+
395+
return $paginator;
396+
}
397+
295398
/**
296399
* newPivot creates a new pivot model instance
297400
*

src/Database/Relations/HasOneOrMany.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public function save(Model $model, $sessionKey = null)
3030
return $model->save() ? $model : false;
3131
}
3232

33+
/**
34+
* saveQuietly saves the supplied related model without raising any events,
35+
* with deferred binding support.
36+
*/
37+
public function saveQuietly(Model $model, $sessionKey = null)
38+
{
39+
return Model::withoutEvents(function () use ($model, $sessionKey) {
40+
return $this->save($model, $sessionKey);
41+
});
42+
}
43+
3344
/**
3445
* saveMany is an alias for the addMany() method
3546
* @param array $models
@@ -42,6 +53,17 @@ public function saveMany($models, $sessionKey = null)
4253
return $models;
4354
}
4455

56+
/**
57+
* saveManyQuietly saves multiple models without raising any events,
58+
* with deferred binding support.
59+
*/
60+
public function saveManyQuietly($models, $sessionKey = null)
61+
{
62+
return Model::withoutEvents(function () use ($models, $sessionKey) {
63+
return $this->saveMany($models, $sessionKey);
64+
});
65+
}
66+
4567
/**
4668
* create a new instance of this related model with deferred binding support
4769
*/
@@ -56,6 +78,76 @@ public function create(array $attributes = [], $sessionKey = null)
5678
return $model;
5779
}
5880

81+
/**
82+
* createQuietly creates a new instance without raising any events,
83+
* with deferred binding support.
84+
*/
85+
public function createQuietly(array $attributes = [], $sessionKey = null)
86+
{
87+
return Model::withoutEvents(function () use ($attributes, $sessionKey) {
88+
return $this->create($attributes, $sessionKey);
89+
});
90+
}
91+
92+
/**
93+
* forceCreateQuietly creates a new instance bypassing mass assignment
94+
* without raising any events, with deferred binding support.
95+
*/
96+
public function forceCreateQuietly(array $attributes = [], $sessionKey = null)
97+
{
98+
return Model::withoutEvents(function () use ($attributes, $sessionKey) {
99+
$model = parent::forceCreate($attributes);
100+
101+
if ($sessionKey !== null) {
102+
$this->add($model, $sessionKey);
103+
}
104+
105+
return $model;
106+
});
107+
}
108+
109+
/**
110+
* createMany creates multiple related models with deferred binding support.
111+
*/
112+
public function createMany(iterable $records, $sessionKey = null)
113+
{
114+
$instances = parent::createMany($records);
115+
116+
if ($sessionKey !== null) {
117+
foreach ($instances as $model) {
118+
$this->add($model, $sessionKey);
119+
}
120+
}
121+
122+
return $instances;
123+
}
124+
125+
/**
126+
* createManyQuietly creates multiple models without raising any events,
127+
* with deferred binding support.
128+
*/
129+
public function createManyQuietly(iterable $records, $sessionKey = null)
130+
{
131+
return Model::withoutEvents(function () use ($records, $sessionKey) {
132+
return $this->createMany($records, $sessionKey);
133+
});
134+
}
135+
136+
/**
137+
* createOrFirst attempts to create the record, or if a unique constraint
138+
* violation occurs, finds the existing record.
139+
*/
140+
public function createOrFirst(array $attributes = [], array $values = [], $sessionKey = null)
141+
{
142+
$model = parent::createOrFirst($attributes, $values);
143+
144+
if ($sessionKey !== null) {
145+
$this->add($model, $sessionKey);
146+
}
147+
148+
return $model;
149+
}
150+
59151
/**
60152
* add a model to this relationship type
61153
*/

0 commit comments

Comments
 (0)