title: Laravel Roles without pivot table
body: <p>I am student and i am new with Laravel and i have this UserController which used to assign multi Roles to User (using pivot table user_role) but i want to assign one role to each user (without pivot table) so i added role_id foreign in table user but i dont know what to change in UserController file.</p>
  
  <p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use App\User;
  use App\Role;
  use DB;
  use Hash;
  
  class UserController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index(Request $request)
      {
          $data = User::orderBy('id','DESC')-&gt;paginate(5);
          return view('users.index',compact('data'))
              -&gt;with('i', ($request-&gt;input('page', 1) - 1) * 5);
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
          $roles = Role::pluck('display_name','id');
          return view('users.create',compact('roles'));
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(Request $request)
      {
          $this-&gt;validate($request, [
              'name' =&gt; 'required',
              'email' =&gt; 'required|email|unique:users,email',
              'password' =&gt; 'required|same:confirm-password',
              'roles' =&gt; 'required'
          ]);
  
          $input = $request-&gt;all();
          $input['password'] = Hash::make($input['password']);
  
          $user = User::create($input);
          foreach ($request-&gt;input('roles') as $key =&gt; $value) {
              $user-&gt;attachRole($value);
          }
  
          return redirect()-&gt;route('users.index')
                          -&gt;with('success','User created successfully');
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
          $user = User::find($id);
          return view('users.show',compact('user'));
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          $user = User::find($id);
          $roles = Role::pluck('display_name','id');
          $userRole = $user-&gt;roles-&gt;pluck('id','id')-&gt;toArray();
  
          return view('users.edit',compact('user','roles','userRole'));
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(Request $request, $id)
      {
          $this-&gt;validate($request, [
              'name' =&gt; 'required',
              'email' =&gt; 'required|email|unique:users,email,'.$id,
              'password' =&gt; 'same:confirm-password',
              'roles' =&gt; 'required'
          ]);
  
          $input = $request-&gt;all();
          if(!empty($input['password'])){ 
              $input['password'] = Hash::make($input['password']);
          }else{
              $input = array_except($input,array('password'));    
          }
  
          $user = User::find($id);
          $user-&gt;update($input);
          DB::table('role_user')-&gt;where('user_id',$id)-&gt;delete();
  
  
          foreach ($request-&gt;input('roles') as $key =&gt; $value) {
              $user-&gt;attachRole($value);
          }
  
          return redirect()-&gt;route('users.index')
                          -&gt;with('success','User updated successfully');
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id)
      {
          User::find($id)-&gt;delete();
          return redirect()-&gt;route('users.index')
                          -&gt;with('success','User deleted successfully');
      }
  }
  </code></pre>
  
  <p>and you can find all the code here:
  <a href="http://itsolutionstuff.com/post/laravel-52-user-acl-roles-and-permissions-with-middleware-using-entrust-from-scratch-tutorialexample.html" rel="nofollow noreferrer">http://itsolutionstuff.com/post/laravel-52-user-acl-roles-and-permissions-with-middleware-using-entrust-from-scratch-tutorialexample.html</a></p>
  
  <p>this mean so much to me if you help me and thanks.</p>