Add "add bookmark" functionality
This commit is contained in:
parent
b12889089b
commit
fb18db918a
@ -25,7 +25,7 @@ class BookmarkController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
//
|
return view('bookmarks.edit', ['bookmark' => null]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,7 +33,19 @@ class BookmarkController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
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(
|
return view(
|
||||||
'bookmarks.edit', [
|
'bookmarks.edit', [
|
||||||
'edit' => true,
|
|
||||||
'bookmark' => $bookmark,
|
'bookmark' => $bookmark,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
@ -71,16 +82,7 @@ class BookmarkController extends Controller
|
|||||||
$bookmark->href = $request->post('href');
|
$bookmark->href = $request->post('href');
|
||||||
$bookmark->save();
|
$bookmark->save();
|
||||||
|
|
||||||
$tags_input = trim($request->post('tags', ''));
|
$bookmark->syncTagsFromString($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"], [
|
||||||
|
@ -15,6 +15,20 @@ class Bookmark extends Model
|
|||||||
|
|
||||||
protected $with = ['tags'];
|
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
|
public function tags(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Tag::class);
|
return $this->belongsToMany(Tag::class);
|
||||||
|
@ -1,39 +1,40 @@
|
|||||||
@extends('layouts.app')
|
@extends('layouts.app')
|
||||||
|
|
||||||
@section('content')
|
@section('content')
|
||||||
|
<h2>{{ $bookmark ? "Edit Bookmark" : "Add Bookmark" }}</h2>
|
||||||
<p>
|
<p>
|
||||||
<a href="{{ action('BookmarkController@index') }}">← Back</a>
|
<a href="{{ action('BookmarkController@index') }}">← Back</a>
|
||||||
</p>
|
</p>
|
||||||
<form method="post" class="form" action="{{ action('BookmarkController@update', ['bookmark' => $bookmark]) }}">
|
<form method="post" class="form" action="{{ $bookmark ? action('BookmarkController@update', ['bookmark' => $bookmark]) : action('BookmarkController@store') }}">
|
||||||
@csrf
|
@csrf
|
||||||
<div class="bookmark">
|
<div class="bookmark">
|
||||||
<div class="bookmark-title form-row"g>
|
<div class="bookmark-title form-row">
|
||||||
<label class="form-label" for="title">
|
<label class="form-label" for="title">
|
||||||
Title:
|
Title:
|
||||||
</label>
|
</label>
|
||||||
<input type="text" class="form-input" name="title" value="{{ $bookmark->title }}">
|
<input type="text" class="form-input" name="title" value="{{ $bookmark?->title }}" tabindex="1">
|
||||||
</div>
|
</div>
|
||||||
<div class="bookmark-href form-row form-row-stacked">
|
<div class="bookmark-href form-row form-row-stacked">
|
||||||
<label class="form-label" for="href">
|
<label class="form-label" for="href">
|
||||||
URL:
|
URL:
|
||||||
</label>
|
</label>
|
||||||
<input type="text" class="form-input" name="href" value="{{ $bookmark->href }}">
|
<input type="text" class="form-input" name="href" value="{{ $bookmark?->href }}" tabindex="3">
|
||||||
</div>
|
</div>
|
||||||
<div class="bookmark-description form-row form-row-stacked">
|
<div class="bookmark-description form-row form-row-stacked">
|
||||||
<label class="form-label" for="description">
|
<label class="form-label" for="description">
|
||||||
Description:
|
Description:
|
||||||
</label>
|
</label>
|
||||||
<textarea name="description" class="form-textarea">{{ $bookmark->description }}</textarea>
|
<textarea name="description" class="form-textarea" tabindex="2">{{ $bookmark?->description }}</textarea>
|
||||||
</div>
|
</div>
|
||||||
<div class="bookmark-tags form-row form-row-stacked">
|
<div class="bookmark-tags form-row form-row-stacked">
|
||||||
<label class="form-label" for="description">
|
<label class="form-label" for="description">
|
||||||
Tags:
|
Tags:
|
||||||
</label>
|
</label>
|
||||||
<input type="text" class="form-input" name="tags" value="@foreach ($bookmark->tags as $tag){{ $tag->name }} @endforeach">
|
<input type="text" class="form-input" name="tags" tabindex="4" value="@foreach ($bookmark?->tags ?? [] as $tag){{ $tag->name }} @endforeach">
|
||||||
</div>
|
</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" tabindex="5" href="{{ $bookmark ? action('BookmarkController@show', ['bookmark' => $bookmark]) : action('BookmarkController@index') }}">Cancel</a>
|
||||||
<input type="submit" class="form-button" value="Save">
|
<input type="submit" tabindex="6" class="form-button" value="Save">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
<header>
|
<header>
|
||||||
<h1><a href="{{ url("/") }}">url snail</a></h1>
|
<h1><a href="{{ url("/") }}">url snail</a></h1>
|
||||||
|
<a href="{{ action('BookmarkController@create') }}">+</a>
|
||||||
</header>
|
</header>
|
||||||
@yield('content')
|
@yield('content')
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user