urlsnail/app/Http/Controllers/BookmarkController.php
2024-05-29 22:25:50 +01:00

100 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Bookmark;
use App\Models\Tag;
use Illuminate\Http\Request;
class BookmarkController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view(
'bookmarks.index', [
'bookmarks' => Bookmark::paginate(20),
]
);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Bookmark $bookmark)
{
return view(
'bookmarks.show', [
'bookmark' => $bookmark,
]
);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Bookmark $bookmark)
{
return view(
'bookmarks.edit', [
'edit' => true,
'bookmark' => $bookmark,
]
);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Bookmark $bookmark)
{
$bookmark->title = $request->post('title');
$bookmark->description = $request->post('description', '');
$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,
]
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}