diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 1d4e35b..6099669 100644 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -3,16 +3,18 @@ namespace App\Http\Controllers; use App\Models\Category; +use App\Models\Link; use App\Models\Topic; use App\Models\User; use Illuminate\Http\Request; class CategoriesController extends Controller { - public function show(Category $category, Request $request, User $user) + public function show(Category $category, Request $request, User $user, Link $link) { $topics = Topic::where('category_id', $category->id)->withOrder($request->order)->with('user', 'category')->paginate(); + $links = $link->getAllCached(); $active_users = $user->getActiveUsers(); - return view('topics.index', compact('topics', 'category', 'active_users')); + return view('topics.index', compact('topics', 'category', 'active_users', 'links')); } } diff --git a/app/Http/Controllers/TopicsController.php b/app/Http/Controllers/TopicsController.php index 8ed2077..9f53b1f 100644 --- a/app/Http/Controllers/TopicsController.php +++ b/app/Http/Controllers/TopicsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Http\Requests\TopicRequest; use App\Models\Category; +use App\Models\Link; use App\Models\Topic; use App\Models\User; use App\Tools\ImageUploadTool; @@ -23,11 +24,12 @@ class TopicsController extends Controller return view('topics.create_and_edit', compact('topic', 'categories')); } - public function index(Request $request, Topic $topic, User $user) + public function index(Request $request, Topic $topic, User $user, Link $link) { $active_users = $user->getActiveUsers(); $topics = $topic->with('user', 'category')->withOrder($request->order)->paginate(); - return view('topics.index', compact('topics', 'active_users')); + $links = $link->getAllCached(); + return view('topics.index', compact('topics', 'links', 'active_users')); } public function show(Topic $topic, Request $request) diff --git a/app/Models/Link.php b/app/Models/Link.php new file mode 100644 index 0000000..30c5513 --- /dev/null +++ b/app/Models/Link.php @@ -0,0 +1,29 @@ +cache_key, $this->cache_expire_in_minutes, function () { + { + return $this->all(); + } + }); + } +} diff --git a/app/Observers/LinkObserver.php b/app/Observers/LinkObserver.php new file mode 100644 index 0000000..b89bd93 --- /dev/null +++ b/app/Observers/LinkObserver.php @@ -0,0 +1,21 @@ +cache_key); + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 943e568..4802275 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,8 +2,10 @@ namespace App\Providers; +use App\Models\Link; use App\Models\Reply; use App\Models\Topic; +use App\Observers\LinkObserver; use App\Observers\ReplyObserver; use App\Observers\TopicObserver; use Carbon\Carbon; @@ -21,7 +23,7 @@ class AppServiceProvider extends ServiceProvider Carbon::setLocale('zh'); Topic::observe(new TopicObserver()); Reply::observe(new ReplyObserver()); - + Link::observe(new LinkObserver()); } /** diff --git a/config/administrator.php b/config/administrator.php index cbc4845..92c0857 100644 --- a/config/administrator.php +++ b/config/administrator.php @@ -1,5 +1,6 @@ [ - 'settings.site' + 'settings.site', + 'links', ] ], diff --git a/config/administrator/links.php b/config/administrator/links.php new file mode 100644 index 0000000..6ff01c3 --- /dev/null +++ b/config/administrator/links.php @@ -0,0 +1,56 @@ + '资源推荐', + 'single' => '资源推荐', + + 'model' => Link::class, + + // 访问权限判断 + 'permission' => function () { + // 只允许站长管理资源推荐链接 + return Auth::user()->hasRole('Founder'); + }, + + 'columns' => [ + 'id' => [ + 'title' => 'ID', + ], + 'title' => [ + 'title' => '名称', + 'sortable' => false, + ], + 'link' => [ + 'title' => '链接', + 'sortable' => false, + ], + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + 'edit_fields' => [ + 'title' => [ + 'title' => '名称', + ], + 'link' => [ + 'title' => '链接', + ], + ], + 'filters' => [ + 'id' => [ + 'title' => '标签 ID', + ], + 'title' => [ + 'title' => '名称', + ], + ], +]; \ No newline at end of file diff --git a/database/factories/LinkFactory.php b/database/factories/LinkFactory.php new file mode 100644 index 0000000..a6c0fc5 --- /dev/null +++ b/database/factories/LinkFactory.php @@ -0,0 +1,10 @@ +define(\App\Models\Link::class, function (Faker $faker) { + return [ + 'title' => $faker->name, + 'link' => $faker->url, + ]; +}); diff --git a/database/migrations/2018_06_02_204347_create_links_table.php b/database/migrations/2018_06_02_204347_create_links_table.php new file mode 100644 index 0000000..ea543b8 --- /dev/null +++ b/database/migrations/2018_06_02_204347_create_links_table.php @@ -0,0 +1,33 @@ +increments('id'); + $table->string('title')->comment('资源的描述')->index(); + $table->string('link')->comment('资源的链接')->index(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('links'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 01f0efa..d8ec93f 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -14,5 +14,6 @@ class DatabaseSeeder extends Seeder $this->call(UsersTableSeeder::class); $this->call(TopicsTableSeeder::class); $this->call(ReplysTableSeeder::class); + $this->call(LinksTableSeeder::class); } } diff --git a/database/seeds/LinksTableSeeder.php b/database/seeds/LinksTableSeeder.php new file mode 100644 index 0000000..f68ca3d --- /dev/null +++ b/database/seeds/LinksTableSeeder.php @@ -0,0 +1,17 @@ +times(6)->make(); + \App\Models\Link::insert($link->toArray()); + } +} diff --git a/resources/views/topics/_sidebar.blade.php b/resources/views/topics/_sidebar.blade.php index 8d47409..8a232f7 100644 --- a/resources/views/topics/_sidebar.blade.php +++ b/resources/views/topics/_sidebar.blade.php @@ -24,4 +24,22 @@ @endforeach +@endif + +@if (count($links)) +
+
+ +
资源推荐
+
+ @foreach ($links as $link) + +
+ {{ $link->title }} +
+
+ @endforeach + +
+
@endif \ No newline at end of file