diff --git a/app/Http/Controllers/BookmarkController.php b/app/Http/Controllers/BookmarkController.php index 8bfe32e..5b37794 100644 --- a/app/Http/Controllers/BookmarkController.php +++ b/app/Http/Controllers/BookmarkController.php @@ -25,7 +25,7 @@ class BookmarkController extends Controller */ public function create() { - // + return view('bookmarks.edit', ['bookmark' => null]); } /** @@ -33,7 +33,19 @@ class BookmarkController extends Controller */ public function store(Request $request) { - // + $bookmark = new Bookmark; + $bookmark->title = $request->post('title'); + $bookmark->description = $request->post('description', ''); + $bookmark->href = $request->post('href'); + $bookmark->save(); + + $bookmark->syncTagsFromString($request->post('tags', '')); + + return redirect()->action( + [self::class, "show"], [ + "bookmark" => $bookmark, + ] + ); } /** @@ -55,7 +67,6 @@ class BookmarkController extends Controller { return view( 'bookmarks.edit', [ - 'edit' => true, 'bookmark' => $bookmark, ] ); @@ -71,16 +82,7 @@ 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)); + $bookmark->syncTagsFromString($request->post('tags', '')); return redirect()->action( [self::class, "show"], [ diff --git a/app/Models/Bookmark.php b/app/Models/Bookmark.php index ebc2078..31e25ee 100644 --- a/app/Models/Bookmark.php +++ b/app/Models/Bookmark.php @@ -15,6 +15,20 @@ class Bookmark extends Model protected $with = ['tags']; + public function syncTagsFromString(string $tags_string): array + { + $tags_input = trim($tags_string); + $tags_input = preg_split('/\s+/', $tags_input); + + $tags = []; + foreach ($tags_input as $tag_input) { + $tag = Tag::firstOrCreate(['name' => $tag_input]); + $tags[$tag->id] = true; + } + + return $this->tags()->sync(array_keys($tags)); + } + public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class); diff --git a/resources/views/bookmarks/edit.blade.php b/resources/views/bookmarks/edit.blade.php index 12bfc27..5c3fd1d 100644 --- a/resources/views/bookmarks/edit.blade.php +++ b/resources/views/bookmarks/edit.blade.php @@ -1,39 +1,40 @@ @extends('layouts.app') @section('content') +

{{ $bookmark ? "Edit Bookmark" : "Add Bookmark" }}

← Back

-
+ @csrf
-
+
- +
- +
- +
- +
- Cancel - + Cancel +
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 96e6cdc..021c494 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -8,6 +8,7 @@

url snail

+ +
@yield('content')