forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c616e0
commit c228771
Showing
12 changed files
with
175 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class Link extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = ['title', 'link']; | ||
|
||
public $cache_key = 'larabbs_links'; | ||
protected $cache_expire_in_seconds = 1440 * 60; | ||
|
||
public function getAllCached() | ||
{ | ||
// 尝试从缓存中取出 cache_key 对应的数据。如果能取到,便直接返回数据。 | ||
// 否则运行匿名函数中的代码来取出 links 表中所有的数据,返回的同时做了缓存。 | ||
return Cache::remember($this->cache_key, $this->cache_expire_in_seconds, function(){ | ||
return $this->all(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Observers; | ||
|
||
use App\Models\Link; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class LinkObserver | ||
{ | ||
// 在保存时清空 cache_key 对应的缓存 | ||
public function saved(Link $link) | ||
{ | ||
Cache::forget($link->cache_key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
], | ||
'站点管理' => [ | ||
'settings.site', | ||
'links', | ||
], | ||
], | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use App\Models\Link; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
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' => '名称', | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class LinkFactory extends Factory | ||
{ | ||
public function definition() | ||
{ | ||
return [ | ||
'title' => $this->faker->name, | ||
'link' => $this->faker->url, | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
database/migrations/2022_03_07_223007_create_links_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('links', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('title')->comment('资源的描述')->index(); | ||
$table->string('link')->comment('资源的链接')->index(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('links'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Database\Seeder; | ||
use App\Models\Link; | ||
|
||
class LinksTableSeeder extends Seeder | ||
{ | ||
public function run() | ||
{ | ||
Link::factory()->times(6)->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters