thedevdojo / voyager

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

Voyager support for dynamic scope query #5300

Open EngBadr opened 3 years ago

EngBadr commented 3 years ago

Version information

Description of problem

Voyager relationship details seems to perfectly support the static scope concept.

In the following scheme I have a table that hold types of objects such as (gender:M,F , institute level : "KG, Primary,",course for each level ).

id type_parent_id type_title
1 Null Institute level
2 1 KG1
3 1 KG3
4 1 Primary
5 1 Undergraduate
6 4 First
7 4 Second
8 4 Third
9 4 ............
10 Null Courses
11 10 Math
12 10 Sciences
13 10 English
14 10 ......
15 Null Gender
16 15 M
17 15 F

In My model I follow the documentation by creating something like

    public function scopeLevel($query)
    {
        return $query->where('type_parent_id', 1);
    }

in Voyager relationship details I post the JSON

{
    "scope": "level"
}

This working fine. However above table will used in many relations and models such as course types , class in each institute ,... etc So, the goal is to generalize scope without hardcoding each type. I think I should use the dynamic scope concept for any type. I need to pass another parameter such as

public function scopeType($query)
 {
   return $query->where('type_parent_id', $gid);
 }
{
    "scope": "type",
    "gid":xxx
}

Where gid is the required id in relationship details. Thus, for institute level I should pass "gid":1
institute of primary school level the course should displayed only "gid":4 .

I think , this should done through using dynamic scope. Is this possible using current Voyager and Laravel versions.

Proposed solution

For now I think I should override the current controller to handle this feature. I 'am not sure about the solution but I will try it. If any better solution will be appreciated.

fletch3555 commented 3 years ago

I don't like the idea of supporting arbitrary scope argument names in the json. That sounds like a maintenance nightmare, not to mention making our scope-handling code now have to dynamically inject an arbitrary number of arguments.

Instead, I think it would be better to handle it much more like how laravel does. So the syntax would be something like:

{
    "scope": "type:1"
}

Having said that, we (internally) have effectively frozen features on Voyager 1 and are focusing development on Voyager 2. Though if someone else wants to put in the effort to add support for this, we'll gladly review/merge/release it.

91Abdullah commented 1 year ago

Any update on this feature request?