Laravel admin from entry to abandonment (3. Multi level linkage selection, backfilling)

Pretend that the previous installation and basic operation of the page are all OK. Now we start to choose the multi-level linkage and backfill of the control, taking the provincial and urban level linkage as the demo,

1, First, multi level linkage selection

In the controller's form() method

$form - > select ('province ',' province ') - > Options ($proviinfo) - > Load ('city', URL ('/ admin / API / cityinfo');

$form - > select ('city ',' urban ') - > Load ('area', URL ('/ admin / API / areainfo');

$form - > select ('area ','district'))

Then add the above url in app/Admin/route.php, put it in front of the current controller or at the top

Create a Model file of Areas, as follows

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Areas extends Model{

    protected $table = 'area';

//    protected $primaryKey = 'id';

    public function table(){
        return $this->table;
    }

    /**
     * @return array
     */
    public function provinceInfo(){
        $provList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE parent_id = -1");
        if($provList){
            $selectOption = [];
            foreach ($provList as $option){
                $selectOption[$option->area_id] = $option->area_name;
            }
            return $selectOption;
        }
        return [];
    }

    /**
     * @return array
     */
    public function childInfo($parentId){
        if(!is_numeric($parentId)){return [];}
        $childList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE parent_id = ".$parentId);
        if($childList){
            $selectOption = [];
            foreach ($childList as $key => $option){
                $selectOption[$key]['id'] = $option->area_id;
                $selectOption[$key]['text'] = $option->area_name;
            }

            return $selectOption;
        }
        return [];
    }

    /**
     * @return array
     */
    public function localInfo($localId){
        if(!is_numeric($localId)){return [];}
        $childList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE area_id = ".$localId);
        if($childList){
            $selectOption = [];
            foreach ($childList as $option){
                $selectOption['id'] = $option->area_id;
                $selectOption['text'] = $option->area_name;
            }
            return $selectOption;
        }
        return [];
    }
}

Add the following method to the controller of the above url

/**
     * City Information
     */
    public function cityInfo(Request $request){
        $q = $request->get('q');
        $special = [1, 19, 792, 2236];

//        $provList =  DB::select("SELECT area_id AS id, `area_name` AS text FROM va_area WHERE parent_id = -1");
//        return Areas::city()->where('parent_id', '=', $q)->paginate(null, ['area_id AS id', 'area_name AS text']);
        $area = new Areas();
        $areaInfo = $area->childInfo($q);
//        if(in_array($q,$special)){ $areaInfo = $area->childInfo($areaInfo[0]['id']); }
        return $areaInfo;
    }

    /**
     * District and County Information
     */
    public function areaInfo(Request $request){
        $q = $request->get('q');
        $area = new Areas();
        return $area->childInfo($q);
    }

 

Form backfill

$area = new Areas();
$provInfo = $area->provinceInfo();
$form->select('province', __('Provinces'))->options($provInfo)->load('city', url('/admin/api/cityInfo'));

$form->select('city', __('urban district'))->options(function ($id){
     $area = new Areas();
     $cityInfo = $area->localInfo($id);
     $cityInfo = [$cityInfo['id']=>$cityInfo['text']];
     return $cityInfo;
})->load('area', url('/admin/api/areaInfo'));

$form->select('area', __('District and county'))->options(function ($id){
      $area = new Areas();
      $cityInfo = $area->localInfo($id);
      $cityInfo = [$cityInfo['id']=>$cityInfo['text']];
      return $cityInfo;
});

Tags: Programming PHP Database

Posted on Wed, 29 Apr 2020 08:19:08 -0700 by djjamiegee