Php 具有字段关系的Laravel背包列表值
我试图列出一个表中的值,该表包含在模型中建立的关系,但显示的是id,而不是与该id相关的名称: District.php和迁移:Php 具有字段关系的Laravel背包列表值,php,laravel,backpack-for-laravel,Php,Laravel,Backpack For Laravel,我试图列出一个表中的值,该表包含在模型中建立的关系,但显示的是id,而不是与该id相关的名称: District.php和迁移: <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Backpack\CRUD\CrudTrait; class District extends Model { use CrudTrait;
<?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');
}
}`
<?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');
}
}
您需要做的是更改此代码:
$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->addColumn
和$this->crud->setColumnDetails
更改为仅$this->crud->setColumnDetails
您需要做的是更改此代码:
$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->addColumn
和$this->crud->setColumnDetails
更改为仅$this->crud->setColumnDetails
$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
]);