thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

I can't filter results on Custom relationship attributes that uses eloquent relationship #4798

Closed GitCodeCruiser closed 4 years ago

GitCodeCruiser commented 4 years ago

Version information

Description

When trying to filter a select results I'm getting "the results could not be loaded". I have a custom attribute in my relationship which is a result of a column from current table and a column from a table in relationship (eloquent relationships).

If I remove column of the table in relationship my custom attribute on Bread relationship filter works just fine.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a custom attribute on the model that includes an attribute of 2nd table using eloquent relationships eg. public $additional_attributes = ['full_name'];

    public function getFullNameAttribute() { return "{$this->name} - {$this->district->name}"; } public function district() { return $this->belongsTo( District::class, 'district_id', 'id'); }

  2. Create a relationship using voyger

  3. Select the custom attribute on the relationship and save

  4. Add new register on BREAD

  5. Try to filter select results

    Expected behavior

    I understand that it's difficult to search in the second table but it must at-least let me filter based on the the current table value.

Screenshots

image image

Additional context

In the screenshots above ملک غلام محمد is the value from current table and لاہور is the value from second table i.e. district

MrCrayon commented 4 years ago

What's the response in ajax call, is there any error?

fleroy91 commented 4 years ago

I've got the same issue and it's coming from there: https://github.com/the-control-group/voyager/blob/1.4/src/Http/Controllers/VoyagerBaseController.php#L873

If you use a scope, you have an Eloquent model and the ->additional_attributes is raising an exception because it works only for model object.

I solved it by overriding the VoyagerBaseControler@relation method and changing this:

if (in_array($options->label, $model->additional_attributes ?? [])) {

by this:

if (in_array($options->label, app($options->model)->additional_attributes ?? [])) {

emptynick commented 4 years ago

I think this is fixed here: https://github.com/the-control-group/voyager/pull/4875

MrCrayon commented 4 years ago

Closed because no feedback was provided.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.