FrozenNode / Laravel-Administrator

An administrative interface package for Laravel
http://administrator.frozennode.com/
MIT License
1.94k stars 504 forks source link

edit_fields in type "relationship" doesn't work when foreign key is a non-primary key. #1048

Open SeptimusLiu opened 7 years ago

SeptimusLiu commented 7 years ago

Hi, it seems that when I use the type "relationship" in edit_fields in model config file, it strangely update the id fields instead of the key group I've set in $this->belongsTo('\App\Models\MtrGroup', 'group', 'group');. What confused me is that it just outputs the right result in columns, but doesn't work with edit_fields and filters. Table schema, eloquent model and model config is as follows, hope you can help me solve it, thx.

Table schema:

CREATE TABLE `mtr_groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order` int(11) NOT NULL DEFAULT '0',
  `group` varchar(20) NOT NULL DEFAULT '',
  `name` varchar(50) NOT NULL DEFAULT '',
  `active` enum('Y','N') NOT NULL DEFAULT 'N',
  `created_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `mtr_mid_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order` int(11) NOT NULL DEFAULT '0',
  `group` varchar(20) NOT NULL DEFAULT '',
  `name` varchar(50) NOT NULL DEFAULT '',
  `created_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;

Eloquent model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MtrGroup extends Model
{
    protected $table = 'mtr_groups';
    public $timestamps = false;
    protected $hidden = ['created_time'];
}
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MtrMidCategory extends Model
{
    protected $table = 'mtr_mid_categories';
    public $timestamps = false;
    protected $hidden = ['order', 'created_time'];

    public function mtr_group()
    {
        return $this->belongsTo('\App\Models\MtrGroup', 'group', 'group');
    }
}

Model config file:

<?php

return [
    'title' => 'Middle Category',

    'single' => 'Middle Category',

    'model' => '\App\Models\MtrMidCategory',

    'columns' => [
        'name' => [
            'title' => 'Name',
        ],
        'group_name' => [
            'title' => 'Group',
            'relationship' => 'mtr_group',
            'select' => "(:table).name",
        ],
        'order' => [
            'title' => 'Order',
        ],
    ],

    'edit_fields' => [
        'name' => [
            'title' => 'Name',
            'type' => 'text',
        ],
        'mtr_group' => [
            'title' => 'Group',
            'type' => 'relationship',
            'name_field' => 'name',
        ],
        'order' => [
            'title' => 'Order',
            'type' => 'number',
        ],
    ],

    'filters' => [
        'name' => [
            'title' => 'Name',
        ],
        'mtr_group' => [
            'title' => 'Group',
            'type' => 'relationship',
            'name_field' => 'name',
        ],
    ],
];

Many mtr_mid_category belong to one group, and related by the key group (not id, it turns to be correct when I use id as foreign key), so why did edit and filter just update and select id? Is there any wrong here?