资源栏

This commit is contained in:
fthvgb1 2018-06-02 21:48:16 +08:00
parent d1e0c21a9a
commit 395b1c619a
12 changed files with 199 additions and 6 deletions

View File

@ -3,16 +3,18 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Category; use App\Models\Category;
use App\Models\Link;
use App\Models\Topic; use App\Models\Topic;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class CategoriesController extends Controller 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(); $topics = Topic::where('category_id', $category->id)->withOrder($request->order)->with('user', 'category')->paginate();
$links = $link->getAllCached();
$active_users = $user->getActiveUsers(); $active_users = $user->getActiveUsers();
return view('topics.index', compact('topics', 'category', 'active_users')); return view('topics.index', compact('topics', 'category', 'active_users', 'links'));
} }
} }

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Http\Requests\TopicRequest; use App\Http\Requests\TopicRequest;
use App\Models\Category; use App\Models\Category;
use App\Models\Link;
use App\Models\Topic; use App\Models\Topic;
use App\Models\User; use App\Models\User;
use App\Tools\ImageUploadTool; use App\Tools\ImageUploadTool;
@ -23,11 +24,12 @@ class TopicsController extends Controller
return view('topics.create_and_edit', compact('topic', 'categories')); 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(); $active_users = $user->getActiveUsers();
$topics = $topic->with('user', 'category')->withOrder($request->order)->paginate(); $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) public function show(Topic $topic, Request $request)

29
app/Models/Link.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
/**
* Class Link
* @property int id
* @property string title
* @property string link
* @package App\Models
*/
class Link extends Model
{
public $cache_key = 'larabbs_key';
public $cache_expire_in_minutes = 1440;
protected $fillable = ['title', 'link'];
public function getAllCached()
{
return Cache::remember($this->cache_key, $this->cache_expire_in_minutes, function () {
{
return $this->all();
}
});
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/2
* Time: 21:36
*/
namespace App\Observers;
use App\Models\Link;
use Cache;
class LinkObserver
{
public function saved(Link $link)
{
Cache::forget($link->cache_key);
}
}

View File

@ -2,8 +2,10 @@
namespace App\Providers; namespace App\Providers;
use App\Models\Link;
use App\Models\Reply; use App\Models\Reply;
use App\Models\Topic; use App\Models\Topic;
use App\Observers\LinkObserver;
use App\Observers\ReplyObserver; use App\Observers\ReplyObserver;
use App\Observers\TopicObserver; use App\Observers\TopicObserver;
use Carbon\Carbon; use Carbon\Carbon;
@ -21,7 +23,7 @@ class AppServiceProvider extends ServiceProvider
Carbon::setLocale('zh'); Carbon::setLocale('zh');
Topic::observe(new TopicObserver()); Topic::observe(new TopicObserver());
Reply::observe(new ReplyObserver()); Reply::observe(new ReplyObserver());
Link::observe(new LinkObserver());
} }
/** /**

View File

@ -1,5 +1,6 @@
<?php <?php
$a = '';
return array( return array(
/** /**
@ -67,7 +68,8 @@ return array(
'replies' 'replies'
], ],
'站点管理' => [ '站点管理' => [
'settings.site' 'settings.site',
'links',
] ]
], ],

View File

@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/2
* Time: 20:55
*/
use App\Models\Link;
return [
'title' => '资源推荐',
'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' => '名称',
],
],
];

View File

@ -0,0 +1,10 @@
<?php
use Faker\Generator as Faker;
$factory->define(\App\Models\Link::class, function (Faker $faker) {
return [
'title' => $faker->name,
'link' => $faker->url,
];
});

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLinksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('links', function (Blueprint $table) {
$table->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');
}
}

View File

@ -14,5 +14,6 @@ class DatabaseSeeder extends Seeder
$this->call(UsersTableSeeder::class); $this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class); $this->call(TopicsTableSeeder::class);
$this->call(ReplysTableSeeder::class); $this->call(ReplysTableSeeder::class);
$this->call(LinksTableSeeder::class);
} }
} }

View File

@ -0,0 +1,17 @@
<?php
use Illuminate\Database\Seeder;
class LinksTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$link = factory(\App\Models\Link::class)->times(6)->make();
\App\Models\Link::insert($link->toArray());
}
}

View File

@ -24,4 +24,22 @@
@endforeach @endforeach
</div> </div>
</div> </div>
@endif
@if (count($links))
<div class="panel panel-default">
<div class="panel-body active-users">
<div class="text-center">资源推荐</div>
<hr>
@foreach ($links as $link)
<a class="media" href="{{ $link->link }}">
<div class="media-body">
<span class="media-heading">{{ $link->title }}</span>
</div>
</a>
@endforeach
</div>
</div>
@endif @endif