Search code examples
laravelvoyager

overriding replationship column to show only users with specific role


I created BREAD for using voyager with relationship column to display all the users and i want this column to show only users with admin role.

I tried to override the view but I think its wrong choice any help?

@if($row->field == 'lead_belongsto_user_relationship')

@endif

I can reach the column in the view/add view but I don't know how to edit the array of results or how to override the query.

It is showing all the users I want to show only admin list.


Solution

  • I finally did it Using custom Controllers. Steps: First create new controller

    php artisan make:controller LeadController
    

    Extend Voyagers controller

    <?php
    
    namespace App\Http\Controllers;
    
    class LeadController extends \TCG\Voyager\Http\Controllers\VoyagerBaseController
    {
        //...
    }
    ?>
    

    Finally you can override the relation function from VoyagerBaseController

            if($request->type == "lead_belongsto_user_relationship"){
                if ($search) {
                    $total_count = app($options->model)->where($options->label, 'LIKE', '%'.$search.'%')->count();
                    $relationshipOptions = app($options->model)->take($on_page)->skip($skip)
                        ->where($options->label, 'LIKE', '%'.$search.'%')->where("role_id",3)
                        ->get();
                } else {
                    $total_count = app($options->model)->count();
                    $relationshipOptions = app($options->model)->take($on_page)->skip($skip)->where("role_id",3)->get();
                }
            }
    

    Here i overrided it to select only users with role_id = 3 you can customize it as you need.

    After that go to the BREAD-settings and fill in the Controller Name with your fully-qualified class-name in my case it is "\App\Http\Controllers\LeadController"