thedevdojo / voyager

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

In Browse view not work searcher when you are trying to search by relation field #5449

Open adpu opened 2 years ago

adpu commented 2 years ago

Laravel version

7.29

PHP version

8.0.10

Voyager version

1.5

Database

Mysql

Description

When I create a relation 'One to Many', after create a relations belongsTo in my bread, in search browse view when I search by relation field, It never show results.

image

Steps to reproduce

**0- Install fresh laravel and voyager

1- Create 2 tables:

Table Recipes:

`<?php

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;

class CreateRecipesTable extends Migration { /**

Table steps:

`<?php

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;

class CreateStepsTable extends Migration { /**

2 - Create Models of tables

Recipe Model

`<?php

namespace App;

use Illuminate\Database\Eloquent\Model; use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use TCG\Voyager\Traits\Resizable; use TCG\Voyager\Traits\Translatable;

class Recipe extends Model { use Resizable; use Translatable;

protected $translatable = ['slug', 'name', 'ingredients', 'video_url', 'metatitle', 'metadescription'];
protected $table = 'recipes';

protected $fillable = [
    'slug', 'name', 'duration',  'level', 'video_url',
    'ingredients', 'related', 'image', 'metatitle', 'metadescription', 'position', 'status'
];

 /*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function steps()
{
    return $this->hasMany('App\Step');
}

} `

Step Model

`<?php

namespace App; use TCG\Voyager\Traits\Translatable; use TCG\Voyager\Traits\Resizable; use Illuminate\Database\Eloquent\Model;

class Step extends Model { use Translatable; use Resizable; protected $translatable = ['text']; protected $table = 'steps';

protected $fillable = [
    'recipe_id',
    'number',
    'text',
    'image'
];
/* -------------------------------------------------------------------------- RELATIONS
*/

public function recipe()
{
    return $this->belongsTo('app\Recipe');
}

}`

3. Create Recipe and Step 'Bread'

Inside my Step 'Bread' I create a relation BelongsTo:

image

4. In my browse view I try to make search by 'recipes_id It not shows results: image

In my database i have registers relationed

Expected behavior

The expected behavior will be the following:

image

Screenshots

No response

Additional context

No response

alexleokyky commented 2 years ago

This is definitly a bug. I found out that in

TCG\Voyager\Http\Controllers\VoyagerBaseController@index

search keys are taken directly from the field data_rows.field.

image This means that when searching in browse we will get a query string like this

?key=product_feedbacks_belongsto_product_relationship& filter=contains&s=test

at the same time in the method VoyagerBaseController :: findSearchableRelationshipRow we check whethersearchKey matches the column of relationship details image

from here

image

this means that in my case the query string should be

?key=product _id&filter=contains&s=test

product _id instead of product_feedbacks_belongsto_product_relationship

such changes helped me to solve the problem temporarily

     $searchNames = [];
        if ($dataType->server_side) {
            $searchNames = $dataType->browseRows->mapWithKeys(function ($row) {
                $searchKey = $row['field'];
                if ($row['type'] == 'relationship') {
                    $searchKey = $row['details']->column;
                }

                return [ $searchKey => $row->getTranslatedAttribute('display_name')];
            });
        }
adpu commented 2 years ago

Thanks, I will study it

Have a nice christmas

El dc., 22 de des. 2021, 18:33, Oleksandr Levytskii < @.***> va escriure:

It is a definitly bug. This is definitly a bug. I found out that in

TCG\Voyager\ @.***

search keys are taken directly from the field data_rows.field.

[image: image] https://user-images.githubusercontent.com/20813103/147129596-cdea9b7a-e3e1-498d-a167-a71b79ea9393.png This means that when searching in browse we will get a query string like this

?key=product_feedbacks_belongsto_product_relationship& filter=contains&s=test

at the same time in the method VoyagerBaseController :: findSearchableRelationshipRow we check whether searchKey matches the column of relationship details [image: image] https://user-images.githubusercontent.com/20813103/147130133-dfe50836-0de3-4b62-8666-da1eaa4a3c26.png

from here

[image: image] https://user-images.githubusercontent.com/20813103/147130729-cfacfacd-999e-405a-a91e-9122f0fa2dba.png

this means that in my case the query string should be

?key=product _id&filter=contains&s=test

product _id instead of product_feedbacks_belongsto_product_relationship

such changes helped me to solve the problem temporarily

    if ($dataType->server_side) {
        $searchNames = $dataType->browseRows->mapWithKeys(function ($row) {
            $searchKey = $row['field'];
            if ($row['type'] == 'relationship') {
                $searchKey = $row['details']->column;
            }

            return [ $searchKey => $row->getTranslatedAttribute('display_name')];
        });
    }```

— Reply to this email directly, view it on GitHub https://github.com/the-control-group/voyager/issues/5449#issuecomment-999749833, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEA6SVDRMOXCJAT2OELUF5DUSIDXPANCNFSM5EXFWH5Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>