diff --git a/README.md b/README.md index bd07617..2451415 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A little project because I haven't made anything new in a while, I would like to * [x] Bookmark index * [x] Bookmark pagination * [x] Bookmark editing (title, description, url) -* [ ] Bookmark tag editing +* [x] Bookmark tag editing * [ ] Tag cloud * [ ] Tag permalink * [ ] Multi-user support diff --git a/app/Http/Controllers/BookmarkController.php b/app/Http/Controllers/BookmarkController.php index 73685c5..8bfe32e 100644 --- a/app/Http/Controllers/BookmarkController.php +++ b/app/Http/Controllers/BookmarkController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Bookmark; +use App\Models\Tag; use Illuminate\Http\Request; class BookmarkController extends Controller @@ -70,6 +71,17 @@ class BookmarkController extends Controller $bookmark->href = $request->post('href'); $bookmark->save(); + $tags_input = trim($request->post('tags', '')); + $tags_input = preg_split('/\s+/', $tags_input); + + $tags = []; + foreach ($tags_input as $tag_input) { + $tag = Tag::firstOrCreate(['name' => $tag_input]); + $tags[$tag->id] = true; + } + + $bookmark->tags()->sync(array_keys($tags)); + return redirect()->action( [self::class, "show"], [ "bookmark" => $bookmark, diff --git a/app/Models/Bookmark.php b/app/Models/Bookmark.php index 00541aa..6408a88 100644 --- a/app/Models/Bookmark.php +++ b/app/Models/Bookmark.php @@ -4,7 +4,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; class Bookmark extends Model { @@ -12,8 +13,8 @@ class Bookmark extends Model protected $table = 'bookmarks'; - public function tags(): HasMany + public function tags(): BelongsToMany { - return $this->hasMany(BookmarkTag::class, 'bookmark_id'); + return $this->belongsToMany(Tag::class); } } diff --git a/app/Models/BookmarkTag.php b/app/Models/BookmarkTag.php index 7db9ab0..5fd89ba 100644 --- a/app/Models/BookmarkTag.php +++ b/app/Models/BookmarkTag.php @@ -3,10 +3,10 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\Pivot; -class BookmarkTag extends Model +class BookmarkTag extends Pivot { use HasFactory; diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 1447cb1..2beef6d 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -4,10 +4,16 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Tag extends Model { use HasFactory; protected $fillable = ['name']; + + public function bookmarks(): BelongsToMany + { + return $this->belongsToMany(Bookmark::class); + } } diff --git a/database/migrations/2024_05_29_200341_unique_bookmark_tag.php b/database/migrations/2024_05_29_200341_unique_bookmark_tag.php new file mode 100644 index 0000000..b3491ce --- /dev/null +++ b/database/migrations/2024_05_29_200341_unique_bookmark_tag.php @@ -0,0 +1,32 @@ +unique(['bookmark_id', 'tag_id']); + } + ); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table( + 'bookmarks_tags', function (Blueprint $table) { + $table->dropUnique('cache_bookmark_id_tag_id_unique'); + } + ); + } +}; diff --git a/database/migrations/2024_05_29_202010_bookmark_tag_table.php b/database/migrations/2024_05_29_202010_bookmark_tag_table.php new file mode 100644 index 0000000..e213ae0 --- /dev/null +++ b/database/migrations/2024_05_29_202010_bookmark_tag_table.php @@ -0,0 +1,31 @@ +foreignId('bookmark_id'); + $table->foreignId('tag_id'); + $table->timestamps(); + $table->primary(['bookmark_id', 'tag_id']); + } + ); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::drop('bookmark_tag'); + } +}; diff --git a/resources/sass/components/_bookmark.scss b/resources/sass/components/_bookmark.scss index f2cb300..6f4d659 100644 --- a/resources/sass/components/_bookmark.scss +++ b/resources/sass/components/_bookmark.scss @@ -9,6 +9,15 @@ "date tags" "actions actions"; + form & { + grid-template-areas: + "title" + "description" + "href" + "tags" + "actions"; + } + margin-bottom: 1em; padding: 0; border: 2px solid #540B0E; @@ -74,9 +83,14 @@ } &-tags { - font-size: 0.8em; grid-area: tags; + font-size: 0.8em; text-align: right; + + form & { + font-size: 0.9em; + text-align: left; + } } &-tag { diff --git a/resources/views/bookmarks/edit.blade.php b/resources/views/bookmarks/edit.blade.php index f1da968..12bfc27 100644 --- a/resources/views/bookmarks/edit.blade.php +++ b/resources/views/bookmarks/edit.blade.php @@ -25,6 +25,12 @@ +
+ + +
Cancel diff --git a/resources/views/components/bookmark.blade.php b/resources/views/components/bookmark.blade.php index fed4b9d..2f12894 100644 --- a/resources/views/components/bookmark.blade.php +++ b/resources/views/components/bookmark.blade.php @@ -18,7 +18,7 @@