在服务提供商中使用Auth时尝试获取非对象的属性

时间:2016-04-01 09:44:21

标签: laravel laravel-5.2

当我使用Auth时,我的服务提供商收到错误'试图获取非对象的属性':

public function boot()
{

    $roles = DB::table('folders')->orderBy('folder_id', 'desc')->where('level', 0)->get();
    if(Auth::user()->level == 1){
        $roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
    }else{
        $user = DB::table('folder_permissions')->where('user_id', Auth::user()->id)->get();
        foreach($user as $u){
            $roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
        }
    }

    $treeFolder = DB::table('folders')->where('level', 0)->get();
    if(!empty($treeFolder)){
        foreach($treeFolder as $folders){
            $arrayCategories[$folders->folder_id] = array("parent_id" => $folders->parent, "name" => array("fname" => $folders->folder_name, "id" => $folders->folder_id));
        }
    }else{
        $arrayCategories = FALSE;  
    }

    view()->share(['folder' => $roles, 'prime_folders' => $roles1, 'treeView' => $arrayCategories]);
}

我已经打电话给'使用Illuminate \ Support \ Facades \ Auth;',但没有任何事情发生。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

看来,Auth在一切准备就绪之前加载。你只需遵循这个,它就可以了

view()->composer('*', function ($view) 
{

    $roles = DB::table('folders')->orderBy('folder_id', 'desc')->where('level', 0)->get();
    if(Auth::user()->level == 1){
        $roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
    }else{
        $user = DB::table('folder_permissions')->where('user_id', Auth::user()->id)->get();
        foreach($user as $u){
            $roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
        }
    }

    $treeFolder = DB::table('folders')->where('level', 0)->get();
    if(!empty($treeFolder)){
        foreach($treeFolder as $folders){
            $arrayCategories[$folders->folder_id] = array("parent_id" => $folders->parent, "name" => array("fname" => $folders->folder_name, "id" => $folders->folder_id));
        }
    }else{
        $arrayCategories = FALSE;  
    }

    //if this line doesn't work then.... see below line after this coming up line
    $view->share(['folder' => $roles, 'prime_folders' => $roles1, 'treeView' => $arrayCategories]);  

    $view->with('folder', $roles)->with('prime_folders',$roles1)->with('treeView',$arrayCategories);  
});