简体   繁体   English

如何在浏览中仅显示来自 Voyager 中当前经过身份验证的用户/作者的帖子?

[英]How to show posts in browse only from current authenticated user/author in Voyager?

I need to display in the Browse option of Voyager the posts only from the user that is logged in in the web.我需要在 Voyager 的浏览选项中显示仅来自登录网络的用户的帖子。 Posts from other users should be hidden.应该隐藏来自其他用户的帖子。 Do I need to make other controller for this or edit the voyagerbreadcontroller.php query based in authenticated user?我是否需要为此制作其他控制器或编辑基于经过身份验证的用户的 voyagerbreadcontroller.php 查询?

Thanks in advance for your help.预先感谢您的帮助。

This works:这有效:

Add to register method of AppServiceProvider.php :添加到AppServiceProvider.php 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:创建扩展 Voyager 模型的新模型,并使用条件 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);
            });
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM