app/Http/Controllers/DepartmentController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $department = Department::create([
            'name' => $request->name
        ]);
        foreach($request->users as $user_id){
            $user = User::find($user_id);
            $user->departments()->attach($department);
            $user->save();
        }
        return redirect()->to(route('departments.index'));
    }
public function store ( Request $ request ) { $ department = Department :: create ( [ 'name' => $ request -> name ] ) ; foreach ( $ request -> users as $ user_id ) { $ user = User :: find ( $ user_id ) ; $ user -> departments ( ) -> attach ( $ department ) ; $ user -> save ( ) ; } return redirect ( ) -> to ( route ( 'departments.index' ) ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .