Laravel背包列出具有场关系的值

时间:2017-06-05 13:13:58

标签: php laravel backpack-for-laravel

我试图列出一个表中的值,这些表包含模型中的关系,但显示的是id而不是与该id相关的名称:

Like here

District.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');
    }

迁移:`

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');
    }
}

迁移:

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
        ]);

1 个答案:

答案 0 :(得分:5)

您需要做的是更改此代码:

$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->crud->addColumn$this->crud->setColumnDetails仅更改为$this->crud->setColumnDetails