Laravel5.6 博客搭建系列四--文章标签后台管理

本文详细介绍了如何在 Laravel5.6 中实现博客的标签管理功能,包括创建标签模型和迁移、控制器及路由设置、标签列表、创建、编辑和删除操作。通过一系列步骤,演示了从数据表设计到前端界面展示的完整过程。
摘要由CSDN通过智能技术生成
创建标签模型和迁移

首先需要创建 Tag 模型类:php artisan make:model --migration Tag该命令会在 app 目录下创建模型文件 Tag.php,由于我们在 make:model 命令中使用了 --migration 选项,所以同时会创建 Tag 模型对应的数据表迁移。在标签(Tag)和文章(Post)之间存在多对多的关联关系,因此还要按照下面的命令创建存放文章和标签对应关系的数据表迁移:php artisan make:migration --create=post_tag_pivot create_post_tag_pivot

在 database/migrations 目录下编辑新创建的标签迁移文件内容如下:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagsTable extends Migration
{
   
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
   
        Schema::create('tags', function (Blueprint $table) {
   
            $table->increments('id');
            $table->string('tag')->unique();
            $table->string('title');
            $table->string('subtitle');
            $table->string('page_image');
            $table->string('meta_description');
            $table->string('layout')->default('blog.layouts.index');
            $table->boolean('reverse_direction');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
   
        Schema::dropIfExists('tags');
    }
}

对上面迁移文件中新增的字段作简要说明:

  • page_image:标签图片
  • meta_description:标签介绍
  • layout:博客终归要使用布局
  • reverse_directions:在文章列表按时间升序排列博客文章(默认是降序)

编辑文章与标签对应关系表迁移文件内容如下:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostTagPivot extends Migration
{
   
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
   
        Schema::create('post_tag_pivot', function (Blueprint $table) {
   
            $table->increments('id');
            $table->integer('post_id')->unsigned()->index();
            $table->integer('tag_id')->unsigned()->index();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
   
        Schema::dropIfExists('post_tag_pivot');
    }
}

运行迁移php artisan migrate

创建控制器添加路由

运行php artisan make:controller Admin\\TagController创建控制器文件。

在routes/web.php文件中,将管理员路由改成以下内容:

Route::namespace('Admin')->middleware(['auth'])->group(function () {
   
    Route::resource('admin/default', 'DefaultController');
    Route::resource('admin/post', 'PostController');
    Route::resource('admin/tag', 'TagController');
});
实现标签列表

在TagController 中添加index方法,具体引入的类可以参照之前的Post。

    public function index()
    {
   
        $tags = Tag::orderBy('created_at', 'desc')
                ->paginate(config('blog.posts_per_page'));
        return view('admin.tag.index')->withTags($tags);
    }

在resources/views/admin/tag下创建index.blade.php视图文件

{% raw %}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值