Skip to content

Commit ef3c871

Browse files
authored
ADD allow saving additional fields via add method (#48)
1 parent be60822 commit ef3c871

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ Like::has($course, $user); // returns whether the given user has marked as liked
141141
Like::count($course); // returns the amount of like marks for the given course
142142
```
143143

144+
### Custom metadata
145+
146+
If needed, you may also add custom metadata when assigning a mark:
147+
148+
``` php
149+
use App\Models\Course;
150+
use Maize\Markable\Models\Like;
151+
152+
$course = Course::firstOrFail();
153+
$user = auth()->user();
154+
155+
Like::add($course, $user, [
156+
'topic' => $course->topic,
157+
]);
158+
159+
Like::toggle($course, $user, [
160+
'topic' => $course->topic,
161+
]);
162+
```
163+
144164
### Custom mark model
145165

146166
The package allows you to define custom marks.

src/Mark.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\MorphPivot;
88
use Illuminate\Database\Eloquent\Relations\MorphTo;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Collection;
1011
use Illuminate\Support\Str;
1112
use Maize\Markable\Exceptions\InvalidMarkableInstanceException;
1213
use Maize\Markable\Exceptions\InvalidMarkValueException;
@@ -15,6 +16,10 @@ abstract class Mark extends MorphPivot
1516
{
1617
public $incrementing = true;
1718

19+
protected $casts = [
20+
'metadata' => 'array',
21+
];
22+
1823
abstract public static function markableRelationName(): string;
1924

2025
public static function markRelationName(): string
@@ -37,7 +42,7 @@ public static function getMarkClassName(): string
3742
->__toString();
3843
}
3944

40-
public static function add(Model $markable, Model $user, string $value = null): self
45+
public static function add(Model $markable, Model $user, string $value = null, array $metadata = []): self
4146
{
4247
static::validMarkable($markable);
4348

@@ -51,9 +56,14 @@ public static function add(Model $markable, Model $user, string $value = null):
5156
'markable_type' => $markable->getMorphClass(),
5257
'value' => $value,
5358
];
54-
$values = static::forceSingleValuePerUser()
55-
? [Arr::pull($attributes, 'value')]
56-
: [];
59+
60+
$values = collect([
61+
'metadata' => $metadata,
62+
])->when(
63+
value: static::forceSingleValuePerUser(),
64+
callback: fn (Collection $values) => $values
65+
->add(Arr::pull($attributes, 'value'))
66+
)->toArray();
5767

5868
return static::firstOrCreate($attributes, $values);
5969
}
@@ -91,11 +101,11 @@ public static function has(Model $markable, Model $user, string $value = null):
91101
])->exists();
92102
}
93103

94-
public static function toggle(Model $markable, Model $user, string $value = null)
104+
public static function toggle(Model $markable, Model $user, string $value = null, array $metadata = [])
95105
{
96106
return static::has($markable, $user, $value)
97107
? static::remove($markable, $user, $value)
98-
: static::add($markable, $user, $value);
108+
: static::add($markable, $user, $value, $metadata);
99109
}
100110

101111
public static function hasAllowedValues(?string $value): bool

tests/MarkTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Maize\Markable\Exceptions\InvalidMarkableInstanceException;
66
use Maize\Markable\Models\Bookmark;
7+
use Maize\Markable\Models\Favorite;
78
use Maize\Markable\Models\Like;
89
use Maize\Markable\Tests\Models\Article;
910
use Maize\Markable\Tests\Models\Post;
@@ -27,6 +28,37 @@ public function can_add_a_mark()
2728
]);
2829
}
2930

31+
/** @test */
32+
public function can_add_metadata()
33+
{
34+
$article = Article::factory()->create();
35+
$user = User::factory()->create();
36+
37+
Favorite::add($article, $user, null, [
38+
'test_data' => true,
39+
]);
40+
41+
$this->assertDatabaseHas((new Favorite)->getTable(), [
42+
'user_id' => $user->getKey(),
43+
'markable_id' => $article->getKey(),
44+
'markable_type' => $article->getMorphClass(),
45+
'value' => null,
46+
'metadata' => json_encode(['test_data' => true]),
47+
]);
48+
49+
Like::toggle($article, $user, null, [
50+
'test_data' => true,
51+
]);
52+
53+
$this->assertDatabaseHas((new Like)->getTable(), [
54+
'user_id' => $user->getKey(),
55+
'markable_id' => $article->getKey(),
56+
'markable_type' => $article->getMorphClass(),
57+
'value' => null,
58+
'metadata' => json_encode(['test_data' => true]),
59+
]);
60+
}
61+
3062
/** @test */
3163
public function cannot_remove_with_an_invalid_markable_type_fail()
3264
{

0 commit comments

Comments
 (0)