【问题标题】:Laravel backpack list values with field relationshipLaravel 背包列表值与字段关系
【发布时间】:2017-06-05 13:13:58
【问题描述】:

我正在尝试从包含模型中建立的关系的表中列出值,但显示的是 id 而不是与该 id 相关的名称:

District.php 和迁移:

<?php

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Backpack\CRUD\CrudTrait;
    
    class District extends Model
    {
        use CrudTrait;
    
         /*
        |--------------------------------------------------------------------------
        | GLOBAL VARIABLES
        |--------------------------------------------------------------------------
        */
    
        protected $table = 'districts';
        protected $primaryKey = 'id';
        // public $timestamps = false;
        // protected $guarded = ['id'];
        protected $fillable = ['name'];
        // protected $hidden = [];
        // protected $dates = [];
    
        /*
        |--------------------------------------------------------------------------
        | FUNCTIONS
        |--------------------------------------------------------------------------
        */
    
        /*
        |--------------------------------------------------------------------------
        | RELATIONS
        |--------------------------------------------------------------------------
        */
        public function county()
        {
            return $this->hasMany('App\Models\County');
        }

迁移:

<?php   
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateDistrictsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('districts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->unique();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('districts');
        }
    }`

County.php

<?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Backpack\CRUD\CrudTrait;
    
    class County extends Model
    {
        use CrudTrait;
    
         /*
        |--------------------------------------------------------------------------
        | GLOBAL VARIABLES
        |--------------------------------------------------------------------------
        */
    
        protected $table = 'counties';
        protected $primaryKey = 'id';
        // public $timestamps = false;
        // protected $guarded = ['id'];
        protected $fillable = ['name', 'DistrictID'];
        // protected $hidden = [];
        // protected $dates = [];
        //$this->table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    
        /*
        |--------------------------------------------------------------------------
        | FUNCTIONS
        |--------------------------------------------------------------------------
        */
    
        /*
        |--------------------------------------------------------------------------
        | RELATIONS
        |--------------------------------------------------------------------------
        */
        public function district() {
            return $this->belongsTo('App\Models\District');
        }
    }

迁移:

<?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateDistrictsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('districts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->unique();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('districts');
        }
    }

CountyCrudController.php

<?php
    
    namespace App\Http\Controllers\Admin;
    
    use Backpack\CRUD\app\Http\Controllers\CrudController;
    
    // VALIDATION: change the requests to match your own file names if you need form validation
    use App\Http\Requests\CountyRequest as StoreRequest;
    use App\Http\Requests\CountyRequest as UpdateRequest;
    
    use App\Models\District as District;
    
    class CountyCrudController extends CrudController
    {
    
        public function setUp()
        {
    
            /*
            |--------------------------------------------------------------------------
            | BASIC CRUD INFORMATION
            |--------------------------------------------------------------------------
            */
            $this->crud->setModel('App\Models\County');
            $this->crud->setRoute(config('backpack.base.route_prefix') . '/county');
            $this->crud->setEntityNameStrings('county', 'counties');
    
            /*
            |--------------------------------------------------------------------------
            | BASIC CRUD INFORMATION
            |--------------------------------------------------------------------------
            */
    
            //$this->crud->setFromDb();
    
            // ------ CRUD FIELDS
            // $this->crud->addField($options, 'update/create/both');
            // $this->crud->addFields($array_of_arrays, 'update/create/both');
            // $this->crud->removeField('name', 'update/create/both');
            // $this->crud->removeFields($array_of_names, 'update/create/both');
    
            $this->crud->addColumn([
                'name' => 'name', // The db column name
                'label' => "County", // Table column heading
                'type' => 'text'
            ]);
    
            $this->crud->addColumn([
                'label' => "District", // Table column heading
                'type' => "text",
                'name' => 'DistrictID', // the column that contains the ID of that connected entity;
                'entity' => 'district', // the method that defines the relationship in your Model
                'attribute' => "name", // foreign key attribute that is shown to user
                'model' => 'App\Models\District' // foreign key model
            ]);
    
            $this->crud->setColumnDetails('DistrictID', ['attribute' => 'name']);
    
            $this->crud->addField([
                'name' => 'name', // The db column name
                'label' => "County" // Table column heading
            ]);
    
            $this->crud->addField([
                'label' => 'District', // Label for HTML form field
                'type'  => 'select2',  // HTML element which displaying transactions
                'name'  => 'DistrictID', // Table column which is FK for Customer table
                'entity'=> 'district', // Function (method) in Customer model which return transactions
                'attribute' => 'name', // Column which user see in select box
                'model' => 'App\Models\District' // Model which contain FK
            ]);

【问题讨论】:

    标签: php laravel laravel-backpack


    【解决方案1】:

    你需要做的是改变这段代码:

    $this->crud->addColumn([
            'label' => "District", // Table column heading
            'type' => "text",
            'name' => 'DistrictID', // the column that contains the ID of that connected entity;
            'entity' => 'district', // the method that defines the relationship in your Model
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => 'App\Models\District' // foreign key model
        ]);
    
    $this->crud->setColumnDetails('DistrictID', ['attribute' => 'name']);
    

    到:

    $this->crud->setColumnDetails([
            'label' => "District", // Table column heading
            'type' => "select",
            'name' => 'DistrictID', // the column that contains the ID of that connected entity;
            'entity' => 'district', // the method that defines the relationship in your Model
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => 'App\Models\District' // foreign key model
        ]);
    

    注意:$this-&gt;crud-&gt;addColumn$this-&gt;crud-&gt;setColumnDetails 仅更改为 $this-&gt;crud-&gt;setColumnDetails

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多