urlsnail/app/Http/Controllers/BookmarkController.php

100 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Bookmark;
2024-05-29 21:07:23 +00:00
use App\Models\Tag;
use Illuminate\Http\Request;
class BookmarkController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
2024-05-25 12:47:48 +00:00
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.
*/
2024-05-26 13:41:28 +00:00
public function show(Bookmark $bookmark)
{
return view(
2024-05-25 12:47:48 +00:00
'bookmarks.show', [
2024-05-26 13:41:28 +00:00
'bookmark' => $bookmark,
]
);
}
/**
* Show the form for editing the specified resource.
*/
2024-05-26 13:41:28 +00:00
public function edit(Bookmark $bookmark)
{
2024-05-26 12:48:04 +00:00
return view(
'bookmarks.edit', [
'edit' => true,
2024-05-26 13:41:28 +00:00
'bookmark' => $bookmark,
2024-05-26 12:48:04 +00:00
]
);
}
/**
* Update the specified resource in storage.
*/
2024-05-26 13:41:28 +00:00
public function update(Request $request, Bookmark $bookmark)
{
2024-05-26 13:41:28 +00:00
$bookmark->title = $request->post('title');
$bookmark->description = $request->post('description', '');
$bookmark->href = $request->post('href');
$bookmark->save();
2024-05-29 21:07:23 +00:00
$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));
2024-05-26 13:41:28 +00:00
return redirect()->action(
[self::class, "show"], [
"bookmark" => $bookmark,
]
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}