Add tag editing

This commit is contained in:
Annika Backstrom 2024-05-29 22:07:23 +01:00
parent 2161e1c226
commit 9a5246374e
Signed by: annika
GPG Key ID: 3561F004DE1D9AFE
10 changed files with 110 additions and 8 deletions

View File

@ -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 index
* [x] Bookmark pagination * [x] Bookmark pagination
* [x] Bookmark editing (title, description, url) * [x] Bookmark editing (title, description, url)
* [ ] Bookmark tag editing * [x] Bookmark tag editing
* [ ] Tag cloud * [ ] Tag cloud
* [ ] Tag permalink * [ ] Tag permalink
* [ ] Multi-user support * [ ] Multi-user support

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Bookmark; use App\Models\Bookmark;
use App\Models\Tag;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class BookmarkController extends Controller class BookmarkController extends Controller
@ -70,6 +71,17 @@ class BookmarkController extends Controller
$bookmark->href = $request->post('href'); $bookmark->href = $request->post('href');
$bookmark->save(); $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( return redirect()->action(
[self::class, "show"], [ [self::class, "show"], [
"bookmark" => $bookmark, "bookmark" => $bookmark,

View File

@ -4,7 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; 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 class Bookmark extends Model
{ {
@ -12,8 +13,8 @@ class Bookmark extends Model
protected $table = 'bookmarks'; protected $table = 'bookmarks';
public function tags(): HasMany public function tags(): BelongsToMany
{ {
return $this->hasMany(BookmarkTag::class, 'bookmark_id'); return $this->belongsToMany(Tag::class);
} }
} }

View File

@ -3,10 +3,10 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\Pivot;
class BookmarkTag extends Model class BookmarkTag extends Pivot
{ {
use HasFactory; use HasFactory;

View File

@ -4,10 +4,16 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Tag extends Model class Tag extends Model
{ {
use HasFactory; use HasFactory;
protected $fillable = ['name']; protected $fillable = ['name'];
public function bookmarks(): BelongsToMany
{
return $this->belongsToMany(Bookmark::class);
}
} }

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table(
'bookmarks_tags', function (Blueprint $table) {
$table->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');
}
);
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create(
'bookmark_tag', function (Blueprint $table) {
$table->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');
}
};

View File

@ -9,6 +9,15 @@
"date tags" "date tags"
"actions actions"; "actions actions";
form & {
grid-template-areas:
"title"
"description"
"href"
"tags"
"actions";
}
margin-bottom: 1em; margin-bottom: 1em;
padding: 0; padding: 0;
border: 2px solid #540B0E; border: 2px solid #540B0E;
@ -74,9 +83,14 @@
} }
&-tags { &-tags {
font-size: 0.8em;
grid-area: tags; grid-area: tags;
font-size: 0.8em;
text-align: right; text-align: right;
form & {
font-size: 0.9em;
text-align: left;
}
} }
&-tag { &-tag {

View File

@ -25,6 +25,12 @@
</label> </label>
<textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea> <textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea>
</div> </div>
<div class="bookmark-tags form-row form-row-stacked">
<label class="form-label" for="description">
Tags:
</label>
<input type="text" class="form-input" name="tags" value="@foreach ($bookmark->tags as $tag){{ $tag->name }} @endforeach">
</div>
<div class="form-row align-right bookmark-actions"> <div class="form-row align-right bookmark-actions">
<a class="form-button" href="{{ action('BookmarkController@show', ['bookmark' => $bookmark]) }}">Cancel</a> <a class="form-button" href="{{ action('BookmarkController@show', ['bookmark' => $bookmark]) }}">Cancel</a>
<input type="submit" class="form-button" value="Save"> <input type="submit" class="form-button" value="Save">

View File

@ -18,7 +18,7 @@
<ul class="bookmark-tags"> <ul class="bookmark-tags">
@foreach ($bookmark->tags as $tag) @foreach ($bookmark->tags as $tag)
<li class="bookmark-tag"> <li class="bookmark-tag">
{{ $tag->tag->name }} {{ $tag->name }}
</li> </li>
@endforeach @endforeach
</ul> </ul>