Laravel admin from getting started to abandoning (I. getting started)

Keywords: Programming Database PHP

1, Installation and release according to official instructions

2, New customized Models and controllers

1. Create a new database table (it is recommended to directly create a table in the database. If you want to try, you can also create a table in migrations. Please refer to the PHP artican command for details)

2. Create models through PHP artican

3. Create a controller and associate the model with PHP artican

model file, which involves table Association

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model{
//    use SoftDeletes;

    //
    protected $table = 'article';

    protected $primaryKey = 'article_id';


    /**
     * Query content of related article index table
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function context(){
        return $this->hasOne(articleContent::class, 'article_id');
    }


    /**
     * Write data
     */
    public function addData(array $dataInfo){
        if (!$dataInfo){return false;}

        $article = array(
            'title'=>$dataInfo['title'],
            'author_id'=>adminInfo()['id'],
            'author_name'=>adminInfo()['name'],
            'keyword'=>$dataInfo['keyword'],
            'describe'=>$dataInfo['describe'],
            'tag'=>$dataInfo['tag'],
            'publish_at'=>$dataInfo['publish_at'],
            'publish_time'=>strtotime($dataInfo['publish_at']),
            'update_at'=>date('Y-m-d H:i:s'),
            'update_time'=>time(),
        );


        $article_idx = array(
            'title'=>$dataInfo['title'],
            'tag'=>$dataInfo['tag'],
            'content'=>$dataInfo['content'],
        );

        //Custom encapsulation transaction, the same below
        begin(); 


        if (isset($dataInfo['article_id'])){

            $article['article_id']      = $dataInfo['article_id'];
            $article_idx['article_id']  = $dataInfo['article_id'];
            // modify
            DB::table('article')->where('article_id',$dataInfo['article_id'])->update($article);
            $ref = DB::table('article_idx')->where('article_id',$dataInfo['article_id'])->update($article_idx);
            $articleId=$dataInfo['article_id'];
        }else{
            // Newly added
            $article['create_at'] = date('Y-m-d H:i:s');
            $article['create_time'] = time();
            $articleId=DB::table("article")->insertGetId($article);
            if(!$articleId){rollBack(); return false;}

            $article_idx['article_id']  = $articleId;
            $ref = DB::table("article_idx")->insert($article_idx);
        }

        if ($articleId AND $ref){ commit(); return true; }
        rollBack(); return false;
    }
}


class articleContent extends Model{

    protected $table = 'article_idx';

    protected $primaryKey = 'article_id';

    public function article(){
        return $this->belongsTo(Article::class, 'article_id');
    }


}

The controller file is not recommended to inherit AdminContrller. I really can't do it

The controller created by the command inherits AdminContrller by default, which can be manually modified by itself. Then, go to AdminContrller to copy index, show, edit and create methods and paste them into the new controller. Now the basic page structure is OK,

4. Add fields to the gird method to display in the list

5. Add the form data to be submitted to the form method, then create the store method to receive the form data, submit the received data to the model file, and create an update method to receive the submission of modified data,

6. Add the fields to be displayed to the detail method

That's the basic use

Posted by ludwig on Wed, 22 Apr 2020 09:25:14 -0700