如何在laravel 5中间件中给出auth条件

时间:2017-07-17 16:20:26

标签: php laravel laravel-5.4

我有一个使用<?php namespace App\Http\Middleware; use Closure; use Menu; use App\Module as modelmodule; use auth; class Frontend { public function handle($request, Closure $next) { Menu::make('sidebar', function($menu){ $menu->add('Main Menu',array('class' => 'header')); //USER// $menu->add("User Control",array('nickname' => "user",'class'=>'treeview')) ->append(' <b class="caret"></b>') ->prepend('<span class="glyphicon glyphicon-user"></span> '); $menu->user->add('Daftar User','user/list'); $menu->user->add('Tipe User','user/type'); //Product// $menu->add("Product",array('nickname' => "product",'class'=>'treeview')) ->append(' <b class="caret"></b>') ->prepend('<span class="glyphicon glyphicon-list"></span> '); $menu->product->add('Product List','user/list'); //About// $menu->add("About Us",array('nickname' => "about")) ->prepend('<span class="glyphicon glyphicon-list"></span> '); }); return $next($request); // } } } 调用的中间件,该中间件的使用显示我使用laravel-menu创建的菜单

auth condition

所以,我的问题是如何添加 if(auth()->user()->isDeveloper()) { $menu->add("About Us",array('nickname' => "about")) ->prepend('<span class="glyphicon glyphicon-list"></span> '); } 例如

auth

但上面的class User extends Authenticatable { use Notifiable; protected $fillable = [ 'name', 'email', 'password','type','subscribestatus','usercode' ]; protected $hidden = [ 'password', 'remember_token', ]; public function isDeveloper() { return ($this->type == 'TP001'); } } 脚本无效。我收到此错误

  

在null

上调用成员函数isDeveloper()

这是我的用户模型

number = benzin_txt.text as Number;

如何修复错误?对不起,我的英语不好 。

1 个答案:

答案 0 :(得分:1)

确保用户在尝试调用其上的函数之前已登录:

if (auth()->check() && auth()->user()->isDeveloper()) {