Laravel admin usage record - quickly build CURD

Introducer

After the installation, simply change the configuration and page effect. Next, build CURD quickly. Use the faker & users table used in the previous test.

Controller and routing

  1. Specify the model generation controller PHP artican admin: make database \ \ fakerusercontroller -- model = app \ \ models \ \ fakeruser. After execution, the file Database/FakerUserController is generated, and related operation methods are available
  2. Add the route in the admin/route.php file, and the final code is as follows
<?php

use Illuminate\Routing\Router;

Admin::registerAuthRoutes();

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

    $router->get('/', 'HomeController@index');

    Route::group(['prefix' => 'database', 'namespace' => 'Database'], function ($route) {
        $route->resource('faker_user', 'FakerUserController');
    });
});

add menu

After code modification, add corresponding menu

After adding, visit

It looks pretty good. CURD has functions, such as export and filter.

Detail optimization

Of course, there are still some shortcomings. For example, when adding, age is a switch here

Simple changes are as follows

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

        // FakerUser::labels() is the corresponding display label
        
        $form->text('name', FakerUser::labels()['name']);
        $form->email('email', FakerUser::labels()['email']);
        $form->number('age', FakerUser::labels()['age']);
        $form->text('city', FakerUser::labels()['city']);

        return $form;
    }

It looks better. Change the following sheet again

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

        $grid->id(FakerUser::labels()['id']);
        $grid->name(FakerUser::labels()['name']);
        $grid->email(FakerUser::labels()['email']);
        $grid->age(FakerUser::labels()['age'])->sortable();// field order
        $grid->city(FakerUser::labels()['city']);
        $grid->created_at(FakerUser::labels()['created_at']);
        $grid->updated_at(FakerUser::labels()['updated_at']);

        // Default reverse order
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function ($filter) {
            // Disable default id filtering
            $filter->disableIdFilter();
            // Name screening
            $filter->like('name', FakerUser::labels()['name']);
            // City screening
            $filter->like('city', FakerUser::labels()['city']);
        });

        return $grid;
    }

epilogue

Please check the rest of the code changes Github . This article has only made simple changes. Laravel admin supports many CURD functions. You can see the official documents.

reference material: Laravel admin documentation.

Tags: PHP Database Laravel REST

Posted on Sat, 30 Nov 2019 20:57:21 -0800 by Zippyaus