Voyager: Is it possible to show only the records assigned to a user?

Created on 19 Mar 2017  路  9Comments  路  Source: the-control-group/voyager

  • Laravel Version: Latest
  • Voyager Version: Latest
  • PHP Version: 5.6.16
  • Database Driver & Version: MySQL 5.6.*

Description:

Hi Everyone,

I'm new to voyager, but have already managed to create tables, populate them and configure their access priviledges for BREAD.

But then I reminded a possible use case: Imagine that I have a tasks table, and that I want all users with role 'User' to see it, BUT I only want them to see the tasks assigned to them (their name or id is a column in the taks table).

Is this possible with voyager or it calls for custom code?
Thanks in advance!

Steps To Reproduce:

Most helpful comment

Figured out a decent solution. Add to _AppServiceProvider@register_:

$this->app->bind('TCG\Voyager\Models\Post', function ($app) {
    return new App\Post;
});

Create the new model that extends Voyager's model, and use a conditional addGlobalScope:

<?php

namespace App;

use TCG\Voyager\Models\Post as VoyagerPost;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

class Post extends VoyagerPost
{
    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

                // Customize your own rule here!
        if (\Request::is('management/*') && Auth::user()->canBlogSolo()) { 
            static::addGlobalScope('author', function (Builder $builder) {
                $builder->where('author_id', '=', Auth::user()->id);
            });
        }
    }
}

That was it @xotik @jonteeter @nunomsh.

All 9 comments

I'm trying to figure out the same thing.

In the meantime I've been able to build a rough workaround by adding a menu entry that points to a custom view where I perform the query, but now I don't know how to put the result in a 'voyager table' with the action buttons, etc. Even if it works, this is not the desired outcome.

Maybe I'm just guessing here, but this is probably a Laravel question, not a Voyager one, since what needs to be changed is the Controller, that needs some new rules like if is admin select all tasks else it's a user so select all tasks where id =

Can anyone point in the right direction please?

Why not simple filter the view results, something like:
@foreach($dataTypeContent->where('author_id','=', Auth::user()->id) as $data)
This will filter the results where the author_id = the logged user.

In the case of the group, you can get if from Auth::user()->role_id

I would check if the logged user as the admin role, and show all the results, else show the result assign to the logged user

This is not an issue with Voyager. Voyager is intended as an Admin tool, not for general use by end-users. What you're trying to do can easily be done with OOTB Laravel functionality and implemented in your site/app's front-end.

I don't agree @fletch3555 , what I'm trying to accomplish is still inside the admin area, I'm just trying to customize the experience for different admin types.

Nevertheless, I agree that this doesn't have to be supported by Voyager OOTB, so custom code is necessary, I'm fine with that.

@nunomsh That's great, I will use it on other occasions, thank you very much for your suggestion. But for now, I would like to filter the results before that (I was thinking on the Voyager controller) and leave the Voyager views untouched, for one reason only: performance (I will only get from the database exactly what I want, not get everything and then filter the results).

Thanks for the support!

Same here, trying to implement how blogger admin type should only be able to view and edit their own posts. That would be a neat tip on the docs!

Figured out a decent solution. Add to _AppServiceProvider@register_:

$this->app->bind('TCG\Voyager\Models\Post', function ($app) {
    return new App\Post;
});

Create the new model that extends Voyager's model, and use a conditional addGlobalScope:

<?php

namespace App;

use TCG\Voyager\Models\Post as VoyagerPost;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

class Post extends VoyagerPost
{
    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

                // Customize your own rule here!
        if (\Request::is('management/*') && Auth::user()->canBlogSolo()) { 
            static::addGlobalScope('author', function (Builder $builder) {
                $builder->where('author_id', '=', Auth::user()->id);
            });
        }
    }
}

That was it @xotik @jonteeter @nunomsh.

@envision this time made me one error, ""Class 'App\Providers\App\Post' not found" but i have the Post extend model on App\Post

@coke00, this has been closed for over a year now. Please either open your own issue or ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings