如何在laravel 5.2中使用用户角色插入数据

时间:2016-09-20 19:26:18

标签: laravel-5.2

我想按用户角色插入数据 这是我的控制器

public function getWorks($class_id,Request $request)
    {
            @if(Auth::user()->role=="Teacher")
            else if($validator->fails())
            {
                $works = new assainments();
                $works -> title = $text;
                //$works -> file = $fileName;
                $works -> class_id = $class_id;
                $works -> users_id = Auth::user()->id;
                $works -> save();

            }
            @endif
       }

我不能在这里使用@if(Auth::user()->role=="Teacher")。我怎么用这个?

1 个答案:

答案 0 :(得分:0)

到目前为止,我认为你做错了什么。以下是概率答案:

public function getWorks($class_id,Request $request)
{
      if(Auth::user()->role=="Teacher")
      {
         // do validation 
         // ....
         // ....

         if( !$validator->fails() )
         {
             $works = new assainments();
             $works -> title = $text;
             //$works -> file = $fileName;
             $works -> class_id = $class_id;
             $works -> users_id = Auth::user()->id;
             $works -> save();

         }
      }
      else
      {
         // do return false
         return redirect()->back();
      }
 }