3
3
namespace Lomkit \Rest \Relations ;
4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
- use Illuminate \Validation \Rule ;
7
6
use Lomkit \Rest \Contracts \QueryBuilder ;
8
7
use Lomkit \Rest \Contracts \RelationResource ;
9
- use Lomkit \Rest \Http \Resource ;
10
8
11
9
class MorphTo extends MorphRelation implements RelationResource
12
10
{
13
- /**
14
- * Create a new MorphTo instance.
15
- *
16
- * @param string $relation The name of the relation.
17
- * @param array $types An array of allowed types for the relation.
18
- */
19
- public function __construct ($ relation , array $ types )
20
- {
21
- $ this ->relation = $ relation ;
22
- $ this ->types = $ types ;
23
- }
24
-
25
11
/**
26
12
* Perform actions before mutating the MorphTo relation.
27
13
*
@@ -34,29 +20,8 @@ public function beforeMutating(Model $model, Relation $relation, array $mutation
34
20
$ model
35
21
->{$ relation ->relation }()
36
22
->{$ mutationRelations [$ relation ->relation ]['operation ' ] === 'detach ' ? 'dissociate ' : 'associate ' }(
37
- app ()->make (QueryBuilder::class, ['resource ' => new $ mutationRelations [ $ relation ->relation ][ ' type ' ] ()])
23
+ app ()->make (QueryBuilder::class, ['resource ' => $ relation ->resource ()])
38
24
->applyMutation ($ mutationRelations [$ relation ->relation ])
39
25
);
40
26
}
41
-
42
- /**
43
- * Define validation rules for the MorphTo relation.
44
- *
45
- * @param resource $resource The resource associated with the relation.
46
- * @param string $prefix The prefix used for validation rules.
47
- *
48
- * @return array An array of validation rules.
49
- */
50
- public function rules (Resource $ resource , string $ prefix )
51
- {
52
- return [
53
- ...parent ::rules ($ resource , $ prefix ),
54
- $ prefix .'.type ' => [
55
- 'required_with: ' .$ prefix ,
56
- Rule::in (
57
- $ this ->types
58
- ),
59
- ],
60
- ];
61
- }
62
27
}
0 commit comments