z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

laravel9的修改器适配下呗! #5740

Open aoeng opened 1 year ago

aoeng commented 1 year ago

详情看这里 https://learnku.com/laravel/t/76071

alexoleynik0 commented 1 year ago

Looks like this is relatively old issue - #5620 , and there's even PR to fix it - #5611. But as this repo is not actively maintained, I can only suggest you to use old Laravel's 8.x and below accessors and mutators. Another advice: to not rewrite all your Models, create extensions of them somewhere in app/Admin/Models. Example:

// in app/Models/User: 
protected function firstName(): \Illuminate\Database\Eloquent\Casts\Attribute{
    return Attribute::make(
        get: fn ($value) => ucfirst($value),
        set: fn ($value) => strtolower($value),
    );
}

// in app/Admin/Models/User:
public function getFirstNameAdminAttribute() {
    return $this->first_name;
}
public function setFirstNameAdminAttribute($value) {
    $this->first_name = $value;
}

// in the Grid/Show/Form now use "app/Admin/Models/User" and "first_name_admin" instead of "app/Models/User" and "first_name".

Not ideal, but should work as a quick fix, if you have few attributes like these and don't want to downgrade main functionality.

Yanghsuanming commented 1 year ago

3 files need to modify

  1. vendor/xn/laravel-admin/src/Grid.php
  2. vendor/xn/laravel-admin/src/Form.php
  3. vendor/xn/laravel-admin/src/Show.php
// Grid.php
LINE:734 
    protected function handleGetMutatorColumn($method, $label)
    {
        $model = $this->model()->eloquent();

        if ($model->hasGetMutator($method)) {
            return $this->addColumn($method, $label);
        }

        if (version_compare(app()->version(), '9.0.0') > 0 && $model->hasAttributeMutator($method)) {
            return $this->addColumn($method, $label);
        }

        return false;
    }
// Form.php
LINE 1100
    /**
     * Get all relations of model from callable.
     *
     * @return array
     */
    public function getRelations(): array
    {
        $relations = $columns = [];

        /** @var Field $field */
        foreach ($this->fields() as $field) {
            $columns[] = $field->column();
        }

        foreach (Arr::flatten($columns) as $column) {
            if (Str::contains($column, '.')) {
                list($relation) = explode('.', $column);

                if (method_exists($this->model, $relation) &&
                    !method_exists(Model::class, $relation) &&
                    $this->model->$relation() instanceof Relations\Relation
                ) {
                    $relations[] = $relation;
                }
            } elseif (method_exists($this->model, $column) &&
                !method_exists(Model::class, $column))
            {
                if (version_compare(app()->version(), '9.0.0') > 0 && $this->model->hasAttributeMutator($column)) {
                    break;
                }
                $relations[] = $column;
            }
        }

        return array_unique($relations);
    }

LINE 480:
    protected function getRelationInputs($inputs = []): array
    {
        $relations = [];

        foreach ($inputs as $column => $value) {
            if ((method_exists($this->model, $column) ||
                method_exists($this->model, $column = Str::camel($column))) &&
                !method_exists(Model::class, $column)
            ) {
                if (version_compare(app()->version(), '9.0.0') > 0 && $this->model->hasAttributeMutator($column)) {
                    continue;
                }
                $relation = call_user_func([$this->model, $column]);

                if ($relation instanceof Relations\Relation) {
                    $relations[$column] = $value;
                }
            }
        }

        return $relations;
    }
// Show.php
LINE 405

    protected function handleGetMutatorField($method, $label)
    {
        if (is_null($this->model)) {
            return false;
        }

        $model = $this->model()->eloquent();

        if ($model->hasGetMutator($method)) {
            return $this->addField($method, $label);
        }

        if (version_compare(app()->version(), '9.0.0') > 0 && $model->hasAttributeMutator($method)) {
            return $this->addField($method, $label);
        }

        return false;
    }