Skip to content

Commit 2928623

Browse files
committed
wip
1 parent 0f282ed commit 2928623

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

src/Fields/BelongsTo.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Fields;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
use Closure;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class BelongsTo extends EagerField
10+
{
11+
public ?Closure $storeParentCallback;
12+
13+
public function storeParent(RestifyRequest $request, Model $child): self
14+
{
15+
if (is_callable($this->storeParentCallback)) {
16+
call_user_func_array($this->storeParentCallback, [
17+
$request,
18+
$child,
19+
]);
20+
21+
return $this;
22+
}
23+
24+
$child->{$this->attribute} = null;
25+
26+
$child->{$this->attribute} = $child->{$this->relationship}()->create(
27+
$request->input($this->attribute)
28+
);
29+
30+
return $this;
31+
}
32+
33+
public function storeParentCallback(callable $callback)
34+
{
35+
$this->storeParentCallback = $callback;
36+
37+
return $this;
38+
}
39+
}

src/Fields/Field.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use Closure;
99
use Illuminate\Contracts\Validation\Rule;
1010
use Illuminate\Support\Str;
11+
use Illuminate\Validation\Rules\Unique;
1112
use JsonSerializable;
1213

13-
/**
14-
* @author Eduard Lupacescu <[email protected]>
15-
*/
1614
class Field extends OrganicField implements JsonSerializable
1715
{
1816
use Make;
@@ -312,21 +310,21 @@ public function getAttribute()
312310
*/
313311
public function storingRules($rules)
314312
{
315-
$this->storingRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
313+
$this->storingRules = ($rules instanceof Rule || is_string($rules) || $rules instanceof Unique) ? func_get_args() : $rules;
316314

317315
return $this;
318316
}
319317

320318
public function storeBulkRules($rules)
321319
{
322-
$this->storingBulkRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
320+
$this->storingBulkRules = ($rules instanceof Rule || is_string($rules) || $rules instanceof Unique) ? func_get_args() : $rules;
323321

324322
return $this;
325323
}
326324

327325
public function updateBulkRules($rules)
328326
{
329-
$this->updateBulkRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
327+
$this->updateBulkRules = ($rules instanceof Rule || is_string($rules) || $rules instanceof Unique) ? func_get_args() : $rules;
330328

331329
return $this;
332330
}
@@ -350,7 +348,7 @@ public function storeRules($rules)
350348
*/
351349
public function updatingRules($rules)
352350
{
353-
$this->updatingRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
351+
$this->updatingRules = ($rules instanceof Rule || is_string($rules) || $rules instanceof Unique) ? func_get_args() : $rules;
354352

355353
return $this;
356354
}
@@ -362,7 +360,7 @@ public function updatingRules($rules)
362360
*/
363361
public function rules($rules)
364362
{
365-
$this->rules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
363+
$this->rules = ($rules instanceof Rule || is_string($rules) || $rules instanceof Unique) ? func_get_args() : $rules;
366364

367365
return $this;
368366
}

src/Fields/HasMany.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Fields;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
use Closure;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class HasMany extends EagerField
10+
{
11+
public ?Closure $storeForParentCallback;
12+
13+
public function storeForParent(RestifyRequest $request, Model $parent): self
14+
{
15+
if (is_callable($this->storeForParentCallback)) {
16+
call_user_func_array($this->storeForParentCallback, [
17+
$request,
18+
$parent
19+
]);
20+
21+
return $this;
22+
}
23+
24+
$parent->{$this->attribute} = collect();
25+
26+
collect(
27+
$request->input($this->attribute)
28+
)->each(function (array $data) use ($parent) {
29+
$parent->{$this->attribute}->push(
30+
$parent->{$this->relationship}()->create($data)
31+
);
32+
});
33+
34+
return $this;
35+
}
36+
37+
public function storeForParentCallback(callable $callback)
38+
{
39+
$this->storeForParentCallback = $callback;
40+
41+
return $this;
42+
}
43+
}
44+

0 commit comments

Comments
 (0)