By Sheldon L | Published at 2020-09-10 | Updated at 2020-09-10 |
// # User
// Auth::routes();
// Route::get('/home', 'HomeController@index')->name('home');
// Route::post('/user/logout', 'Auth\LoginController@userLogout')->name('user.logout');
# Admin
Route::prefix('admin')->group(function () {
# Dashboard
Route::get('/', 'AdminController@index')->name('admin.dashboard');
# Login and Logout
Route::get('/login', 'Auth\AdminLoginController@showLoginForm')->name('admin.login');
Route::post('/login', 'Auth\AdminLoginController@login')->name('admin.login.submit');
Route::post('/logout', 'Auth\AdminLoginController@logout')->name('admin.logout');
# No register, only can add admin in tinker for safe consideration
# Reset password
// Route::get('/password/reset', 'Auth\AdminForgotPasswordController@showLinkRequestForm')->name('admin.password.request');
// Route::post('/password/email', 'Auth\AdminForgotPasswordController@sendResetLinkEmail')->name('admin.password.email');
// Route::get('/password/reset/{token}', 'Auth\AdminResetPasswordController@showResetForm')->name('admin.password.reset');
// Route::post('/password/reset', 'Auth\AdminResetPasswordController@reset')->name('admin.password.update');
# CMS
Route::resource('categories', 'CategoryController');
Route::resource('authors', 'AuthorController');
Route::resource('posts', 'PostController');
Route::resource('pages', 'PageController');
Route::resource('news', 'NewsController');
Route::resource('tags', 'TagController');
Route::resource('photos', 'PhotoController');
});
php artisan make:controller CategoryController
php artisan make:controller AuthorController
php artisan make:controller PostController
php artisan make:controller PageController
php artisan make:controller NewsController
php artisan make:controller PhotoController
php artisan make:controller TagController
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.category_list');
$columns = Category::orderBy('name', 'ASC')->where('is_column', '1')->get();
return view('admin.category.index', compact('title', 'columns'));
}
public function create()
{
$title = trans('admin_CRUD.create_category');
$columns = Category::latest()->where('is_column', '1')->pluck('name', 'id');
return view('admin.category.create', compact('title', 'columns'));
}
public function store(CategoryRequest $request)
{
$validated = $request->validated();
$data = [
'admin_id' => Auth::id(),
'thumbnail' => $validated['thumbnail'],
'name' => ['cn'=>$validated['name_cn'], 'en'=>$validated['name_en']],
'slug' => $validated['slug'],
'is_column' => $request->is_column,
'is_published' => $request->is_published,
];
if ($request->category_id){
$column = Category::find($validated['category_id']);
$column->categories()->create($data);
} else {
Category::create($data);
}
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('categories.index');
}
public function show(Category $category)
{
//
}
public function edit(Category $category)
{
$title = trans('admin_CRUD.update_category');
$columns = Category::latest()->where('is_column', '1')->pluck('name', 'id');
$column_belonging_id = null;
if ($category->category()->first()) {
$column_belonging_id = $category->category()->first()->id;
}
return view('admin.category.edit', compact('title', 'category', 'columns', 'column_belonging_id'));
}
public function update(CategoryRequest $request, Category $category)
{
$validated = $request->validated();
$category->admin_id = Auth::id();
$category->thumbnail = $validated['thumbnail'];
$category->name = ['cn'=>$validated['name_cn'], 'en'=>$validated['name_en']];
$category->slug = $validated['slug'];
$category->is_column = $request->is_column;
$category->is_published = $request->is_published;
if ($request->category_id) {
$column = Category::find($validated['category_id']);
$category->category()->associate($column);
} else {
try {
$category->category()->dissociate();
}
catch (Exception $e) {
return;
}
}
$category->save();
$category->refresh();
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('categories.index');
}
public function destroy(Category $category)
{
$category->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('categories.index');
}
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.author_list');
$authors = Author::latest()->get();
return view('admin.author.index', compact('title', 'authors'));
}
public function create()
{
$title = trans('admin_CRUD.create_author');
return view('admin.author.create', compact('title'));
}
public function store(AuthorRequest $request)
{
$validated = $request->validated();
Author::create([
'admin_id' => Auth::id(),
'thumbnail' => $validated['thumbnail'],
'name' => ['cn'=>$validated['name_cn'], 'en'=>$validated['name_en']],
'slug' => $validated['slug'],
'intro' => ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']],
'is_published' => $request->is_published,
]);
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('authors.index');
}
public function show(Author $author)
{
//
}
public function edit(Author $author)
{
$title = trans('admin_CRUD.update_author');
return view('admin.author.edit', compact('title', 'author'));
}
public function update(AuthorRequest $request, Author $author)
{
$validated = $request->validated();
$author->admin_id = Auth::id();
$author->thumbnail = $validated['thumbnail'];
$author->name = ['cn'=>$validated['name_cn'], 'en'=>$validated['name_en']];
$author->slug = $validated['slug'];
$author->intro = ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']];
$author->is_published = $request->is_published;
$author->save();
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('authors.index');
}
public function destroy(Author $author)
{
$author->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('authors.index');
}
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.post_list');
$posts = Post::latest()->where('post_type', 'post')->get();
return view('admin.post.index', compact('title', 'posts'));
}
public function create()
{
$title = trans('admin_CRUD.create_post');
$authors = Author::orderBy('name', 'ASC')->pluck('name', 'id');
$categories = Category::orderBy('name', 'ASC')->where('is_column', '0')->pluck('name', 'id');
$tags = Tag::orderBy('name', 'ASC')->pluck('name', 'id');
return view('admin.post.create', compact('title', 'authors', 'categories', 'tags'));
}
public function store(PostRequest $request)
{
$validated = $request->validated();
$data = [
'admin_id' => Auth::id(),
'thumbnail' => $validated['thumbnail'],
'title' => ['cn' => $validated['title_cn'], 'en' => $validated['title_en']],
'slug' => $validated['slug'],
'sub_title' => ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']],
'is_top' => $request->is_top,
'limit' => '0',
'is_reproduced' => $request->is_reproduced,
'source' => $validated['source'],
'source_url' => $validated['source_url'],
'editor' => $validated['editor'],
'intro' => ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']],
'details' => ['cn' => $validated['details_cn'], 'en' => $validated['details_en']],
'is_published' => $request->is_published,
'post_type' => 'post',
'created_at' => $request->created_at,
];
if ($request->category_id){
$category = Category::find($validated['category_id']);
$post = $category->posts()->create($data);
} else {
$post = Post::create($data);
}
$post->authors()->sync($request->author_id, true);
$post->tags()->sync($request->tag_id, true);
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('posts.index');
}
public function show(Post $post)
{
//
}
public function edit(Post $post)
{
$title = trans('admin_CRUD.update_post');
$authors = Author::orderBy('name', 'ASC')->pluck('name', 'id');
$categories = Category::orderBy('name', 'ASC')->where('is_column', '0')->pluck('name', 'id');
$tags = Tag::orderBy('name', 'ASC')->pluck('name', 'id');
$category_belonging_id = null;
if ($post->category()->first()) {
$category_belonging_id = $post->category()->first()->id;
}
return view('admin.post.edit', compact('title', 'authors', 'categories', 'tags', 'post', 'category_belonging_id'));
}
public function update(PostRequest $request, Post $post)
{
$validated = $request->validated();
$post->admin_id = Auth::id();
$post->thumbnail = $validated['thumbnail'];
$post->title = ['cn' => $validated['title_cn'], 'en' => $validated['title_en']];
$post->slug = $validated['slug'];
$post->sub_title = ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']];
$post->is_top = $request->is_top;
$post->limit = '0';
$post->is_reproduced = $request->is_reproduced;
$post->source = $validated['source'];
$post->source_url = $validated['source_url'];
$post->editor = $validated['editor'];
$post->intro = ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']];
$post->details = ['cn' => $validated['details_cn'], 'en' => $validated['details_en']];
$post->is_published = $request->is_published;
$post->post_type = 'post';
$post->created_at = $request->created_at;
if ($request->category_id) {
$category = Category::find($validated['category_id']);
$post->category()->associate($category);
} else {
try {
$post->category()->dissociate();
}
catch (Exception $e) {
return;
}
}
$post->save();
$post->authors()->sync($request->author_id, true);
$post->tags()->sync($request->tag_id, true);
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('posts.index');
}
public function destroy(Post $post)
{
$post->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('posts.index');
}
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.page_list');
$pages = Post::latest()->where('post_type', 'page')->get();
return view('admin.page.index', compact('title', 'pages'));
}
public function create()
{
$title = trans('admin_CRUD.create_page');
return view('admin.page.create', compact('title'));
}
public function store(PageRequest $request)
{
$validated = $request->validated();
if (!$request->created_at) {
$request->created_at = Carbon::now();
}
Post::create([
'admin_id' => Auth::id(),
'thumbnail' => $validated['thumbnail'],
'title' => ['cn' => $validated['title_cn'], 'en' => $validated['title_en']],
'slug' => $validated['slug'],
'sub_title' => ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']],
'is_top' => $request->is_top,
'details' => ['cn' => $validated['details_cn'], 'en' => $validated['details_en']],
'is_published' => $request->is_published,
'post_type' => 'page',
'created_at' => $request->created_at,
]);
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('pages.index');
}
public function show(Post $page){
}
public function edit(Post $page)
{
$title = trans('admin_CRUD.update_news');
return view('admin.page.edit', compact('title', 'page'));
}
public function update(PageRequest $request, Post $page)
{
$validated = $request->validated();
if (!$request->created_at) {
$request->created_at = Carbon::now();
}
$page->admin_id = Auth::id();
$page->thumbnail = $validated['thumbnail'];
$page->title = ['cn' => $validated['title_cn'], 'en' => $validated['title_en']];
$page->slug = $validated['slug'];
$page->sub_title = ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']];
$page->is_top = $request->is_top;
$page->details = ['cn' => $validated['details_cn'], 'en' => $validated['details_en']];
$page->is_published = $request->is_published;
$page->post_type = 'page';
$page->created_at = $request->created_at;
$page->save();
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('pages.index');
}
public function destroy(Post $page)
{
$page->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('pages.index');
}
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.news_list');
$newses = Post::latest()->where('post_type', 'news')->get();
return view('admin.news.index', compact('title', 'newses'));
}
public function create()
{
$title = trans('admin_CRUD.create_news');
return view('admin.news.create', compact('title'));
}
public function store(NewsRequest $request)
{
$validated = $request->validated();
Post::create([
'admin_id' => Auth::id(),
'thumbnail' => $validated['thumbnail'],
'title' => ['cn' => $validated['title_cn'], 'en' => $validated['title_en']],
'slug' => $validated['slug'],
'sub_title' => ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']],
'is_top' => $request->is_top,
'details' => ['cn' => $validated['details_cn'], 'en' => $validated['details_en']],
'is_published' => $request->is_published,
'post_type' => 'news',
]);
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('news.index');
}
public function show(Post $news)
{
//
}
public function edit(Post $news)
{
$title = trans('admin_CRUD.update_news');
return view('admin.news.edit', compact('title', 'news'));
}
public function update(NewsRequest $request, Post $news)
{
$validated = $request->validated();
$news->admin_id = Auth::id();
$news->thumbnail = $validated['thumbnail'];
$news->title = ['cn' => $validated['title_cn'], 'en' => $validated['title_en']];
$news->slug = $validated['slug'];
$news->sub_title = ['cn' => $validated['sub_title_cn'], 'en' => $validated['sub_title_en']];
$news->is_top = $request->is_top;
$news->details = ['cn' => $validated['details_cn'], 'en' => $validated['details_en']];
$news->is_published = $request->is_published;
$news->post_type = 'news';
$news->save();
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('news.index');
}
public function destroy(Post $news)
{
$news->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('news.index');
}
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.photo_list');
$photos = Photo::latest()->get();
return view('admin.photo.index', compact('title', 'photos'));
}
public function create()
{
$title = trans('admin_CRUD.photo_upload');
$tags = Tag::orderBy('name', 'ASC')->pluck('name', 'id');
return view('admin.photo.create', compact('title', 'tags'));
}
public function store(PhotoRequest $request)
{
$validated = $request->validated();
foreach ($validated['image_url'] as $image_url) {
$current_timestamp = Carbon::now()->timestamp;
$fileNameWithExt = $image_url->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$fileExt = $image_url->getClientOriginalExtension();
$fileNameToStore = $fileName . '_' . $current_timestamp . '.' . $fileExt;
$photo = new Photo();
$photo->admin_id = Auth::id();
$photo->image_url = $fileNameToStore;
$photo->intro = ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']];
$photo->is_published = $request->is_published;
$save = $photo->save();
$photo->tags()->sync($request->tag_id, true);
if ($save) {
$image_url->storeAs('public/photos', $fileNameToStore);
}
}
Session::flash('message', trans('admin_CRUD.image_uploaded_successfully'));
return redirect()->route('photos.index');
}
public function show(Photo $photo)
{
}
public function edit(Photo $photo)
{
$title = trans('admin_CRUD.update_photo_info');
$tags = Tag::orderBy('name', 'ASC')->pluck('name', 'id');
return view('admin.photo.edit', compact('title', 'photo', 'tags'));
}
public function update(PhotoRequest $request, Photo $photo)
{
$validated = $request->validated();
$photo->admin_id = Auth::id();
$photo->intro = ['cn' => $validated['intro_cn'], 'en' => $validated['intro_en']];
$photo->is_published = $request->is_published;
$photo->save();
$photo->tags()->sync($request->tag_id, true);
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('photos.index');
}
public function destroy(Photo $photo)
{
Storage::delete('public/photos/' . $photo->image_url);
$photo->delete();
Session::flash('danger-message', trans('admin_CRUD.images_deleted_successfully'));
return redirect()->route('photos.index');
}
php artisan storage:link
public function __construct()
{
$this->middleware('auth:admin');
}
public function index()
{
$title = trans('admin_CRUD.tag_list');
$tags = Tag::orderBy('name', 'ASC')->get();
return view('admin.tag.index', compact('title', 'tags'));
}
public function create()
{
$title = trans('admin_CRUD.create_tag');
return view('admin.tag.create', compact('title'));
}
public function store(TagRequest $request)
{
$validated = $request->validated();
Tag::create([
'admin_id' => Auth::id(),
'name' => ['cn'=>$validated['name_cn'], 'en'=>str_replace('-', '', $validated['slug'])],
'slug' => $validated['slug'],
]);
Session::flash('message', trans('admin_CRUD.created_successfully'));
return redirect()->route('tags.index');
}
public function show(Tag $tag)
{
//
}
public function edit(Tag $tag)
{
$title = trans('admin_CRUD.update_tag');
return view('admin.tag.edit', compact('title', 'tag'));
}
public function update(TagRequest $request, Tag $tag)
{
$validated = $request->validated();
$tag->admin_id = Auth::id();
$tag->name = ['cn' => $validated['name_cn'], 'en' => $validated['name_en']];
$tag->slug = $validated['slug'];
$tag->save();
Session::flash('warning-message', trans('admin_CRUD.updated_successfully'));
return redirect()->route('tags.index');
}
public function destroy(Tag $tag)
{
$tag->delete();
Session::flash('danger-message', trans('admin_CRUD.deleted_successfully'));
return redirect()->route('tags.index');
}