laravel-admin 创建自己的数据库并生成控制器

使用migration来使用

以blog两个表为例

  1. 生成迁移:$ php artisan make:migration create_blog_table
     在 database/migration 中生成迁移文件,可对迁移文件进行编辑,创建自己想要的数据表


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

class CreateBlogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blog_category', function (Blueprint $table) {
            $table->bigIncrements('cate_id');
            $table->string('cate_name','50');
            $table->string('cate_title','255');
            $table->smallInteger('cate_order');
            $table->Integer('cate_pid');
            $table->softDeletes();
            $table->timestamps();
        });
        Schema::create('blog_article', function (Blueprint $table) {
            $table->bigIncrements('art_id');
            $table->string('art_title','100');
            $table->string('art_tag','100');
            $table->string('art_description','255');
            $table->string('art_thumb','255');
            $table->text('art_content');
            $table->timestamp('art_time');
            $table->string('art_editor','50');
            $table->Integer('art_view');
            $table->Integer('cate_id');
            $table->Integer('cate_status');
            $table->Integer('cate_love');
            $table->Integer('cate_collect');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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

2. 运行迁移:php artisan migrate

运行生成的user表的迁移文件

3.创建Blog.php文件

最好是把model放在一个文件夹中比如 我们放入 app下的admin里

php artisan make:model /Admin/Model/Blog
注意这里我们在使用artisan命令 我们的 / 指向的就是app!

4.添加控制器


php artisan admin:make BlogController --model=App\Admin\Model\blog

注意在laravel admin 控制台源代码中有展示,这个linux下两个\ 反斜杠是转义 和路径
所以这里都用两个反斜杠
 生成 UserController.php 文件

  1. 添加资源路由
    在 app/Admin/routes.php:
$router->resource('blogs', BlogController::class); 

  1. 代码生成情况展示
    控制器:


namespace App\Admin\Controllers;

use App\Admin\Model\blog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BlogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Admin\Model\blog';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new blog);



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(blog::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new blog);



        return $form;
    }
}

路由:



use Illuminate\Routing\Router;

Admin::routes();

Route::group([
    'prefix'        => config('admin.route.prefix'),
    'namespace'     => config('admin.route.namespace'),
    'middleware'    => config('admin.route.middleware'),
], function (Router $router) {

    $router->get('/', 'HomeController@index')->name('admin.home');
    $router->resource('users', UserController::class);
    $router->resource('blogs', BlogController::class);


});
 

你可能感兴趣的:(laravel)