Skip to content

Instantly share code, notes, and snippets.

@malkafly
Created November 29, 2018 12:56
Show Gist options
  • Save malkafly/e08c56eb0f2438556e4272a634ae6073 to your computer and use it in GitHub Desktop.
Save malkafly/e08c56eb0f2438556e4272a634ae6073 to your computer and use it in GitHub Desktop.
Laravel Voyager - Show only the records assigned to a user

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);
			});
		}
	}
}
@malkafly
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment