Add tag editing
This commit is contained in:
parent
2161e1c226
commit
9a5246374e
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
31
database/migrations/2024_05_29_202010_bookmark_tag_table.php
Normal file
31
database/migrations/2024_05_29_202010_bookmark_tag_table.php
Normal 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');
|
||||
}
|
||||
};
|
@ -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 {
|
||||
|
@ -25,6 +25,12 @@
|
||||
</label>
|
||||
<textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea>
|
||||
</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">
|
||||
<a class="form-button" href="{{ action('BookmarkController@show', ['bookmark' => $bookmark]) }}">Cancel</a>
|
||||
<input type="submit" class="form-button" value="Save">
|
||||
|
@ -18,7 +18,7 @@
|
||||
<ul class="bookmark-tags">
|
||||
@foreach ($bookmark->tags as $tag)
|
||||
<li class="bookmark-tag">
|
||||
{{ $tag->tag->name }}
|
||||
{{ $tag->name }}
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
|
Loading…
Reference in New Issue
Block a user