본문 바로가기
iaa.dev/Laravel8

Laravel 8 관리자 화면 - CRUD 생성,수정,삭제 기능

by chopper.kid 2022. 1. 10.

이전글에서는 Policy 확인차 리스트로 표시하고 안하고 정도만 확인할 수 있는 기능을 보여줬습니다만 

이번글에서 기본적인 CRUD 의 기능을 다 넣어보겠습니다.

우선 이전글까지가 바탕이 되어야 합니다.

 

 

 

Laravel 8 관리자 화면 - 관리자 및 회원관리

이전까지의 관리화면 만드는 부분에 이어서 이번에는 관리자 / 회원 리스트 표시및 권한에 따라 화면을 표시할지 말지를 결정할 Policy 에 대해 설명합니다.. 이미 많은 role , permission 등을 다루는

vuxy.tistory.com

Route 설정

routes/admin.php 

<?php

use Illuminate\Support\Facades\Route;

Route::get('/login',    'App\Http\Controllers\Admin\AdminLoginController@showLoginForm')->name('login');
Route::post('/login',   'App\Http\Controllers\Admin\AdminLoginController@login')->name('login.submit');
Route::get('logout/',   'App\Http\Controllers\Admin\AdminLoginController@logout')->name('logout');
Route::get('/',         'App\Http\Controllers\Admin\DashboardController@dashboard')->name('dashboard');


Route::resource('users',    'App\Http\Controllers\Admin\UserController');
Route::resource('admins',   'App\Http\Controllers\Admin\AdminController');

 

config 파일 설정

config/adminlte.php 의 'menu' 값을 다음과 같게 합니다.

    'menu' => [
        // Navbar items:
        [
            'type'         => 'navbar-search',
            'text'         => 'search',
            'topnav_right' => true,
        ],
        [
            'type'         => 'fullscreen-widget',
            'topnav_right' => true,
        ],

        // Sidebar items:
        [
            'type' => 'sidebar-menu-search',
            'text' => 'search',
        ],
        [
            'text' => 'Dashboard',
            'url'  => 'admin',
            'icon' => 'fas fa-tachometer-alt',
        ],
        [
            'text' => 'Users',
            'url'  => 'admin/users',
            'icon' => 'fas fa-fw fa-user',
        ],
        [
            'text' => 'Admin',
            'url'  => 'admin/admins',
            'icon' => 'fas fa-fw fa-user',
        ],

config/app_admin.php 작성

<?php

return [

    'prefix' => env('APP_ADMIN_PREFIX', 'admin'), // NEW
    'admin_types' => [
        'super_admin' => 'Super Administrator',
        'admin'     => 'Administrator',
        'moderator' => 'Moderator',
        'editor'    => 'Editor',
    ],


];

 

컨트롤러 수정

app/Http/Controllers/Admin/AdminController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Http\Requests\StoreAdminRequest;
use App\Http\Requests\UpdateAdminRequest;

class AdminController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->authorizeResource(Admin::class, 'admin');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $models = Admin::paginate();
        \Debugbar::info($models);

        return view(
            "admin.admins.index" ,
            [
                'models' => $models,
            ]
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $types = config('app_admin.admin_types');
        
        return view(
            "admin.admins.create" ,
            [
                'types' => $types
            ]
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreAdminRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAdminRequest $request)
    {
        $validated = $request->validated();
        $validated['password'] = bcrypt($validated['password']);

        $admin = Admin::create($validated);

        return redirect('admin/admins/')->with('success', 'Admin Created Successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function show(Admin $admin)
    {
        $types = config('app_admin.admin_types');
        
        return view(
            "admin.admins.show" ,
            [
                'model' => $admin ,
                'types' => $types

            ]
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function edit(Admin $admin)
    {
        $types = config('app_admin.admin_types');
        return view(
            "admin.admins.edit" ,
            [
                'model' => $admin ,
                'types' => $types

            ]
        );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateAdminRequest  $request
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAdminRequest $request, Admin $admin)
    {
        $validated = $request->validated();

        $admin->name = $request->name;
        $admin->email = $request->email;
        $admin->type = $request->type;
        $admin->save();

        return redirect('admin/admins/'.$admin->id)->with('success', 'Admin Updated Successfully.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Http\Response
     */
    public function destroy(Admin $admin)
    {
        
        $admin->delete();
        return redirect('admin/admins')->with('success', 'Admin Removed Successfully.');
    }
}

app/Http/Controllers/Admin/UserController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;

class UserController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->authorizeResource(User::class, 'user');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $models = User::paginate();
        \Debugbar::info($models);

        return view(
            "admin.users.index" ,
            [
                'models' => $models,
            ]
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view(
            "admin.users.create" ,
            [
            ]
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreUserRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUserRequest $request)
    {
        $validated = $request->validated();
        $validated['password'] = bcrypt($validated['password']);

        $admin = User::create($validated);

        return redirect('admin/users/')->with('success', 'User Created Successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return view(
            "admin.users.show" ,
            [
                'model' => $user 
            ]
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        return view(
            "admin.users.edit" ,
            [
                'model' => $user 

            ]
        );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateUserRequest  $request
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateUserRequest $request, User $user)
    {
        $validated = $request->validated();

        $user->name  = $request->name;
        $user->email = $request->email;
        $user->save();

        return redirect('admin/users/'.$user->id)->with('success', 'User Updated Successfully.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\User $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        $user->delete();
        return redirect('admin/users')->with('success', 'User Removed Successfully.');
    }
}

app/Http/Controllers/Admin/Controller.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as AdminBaseController;

class Controller extends AdminBaseController
{

    public function __construct()
    {
        $this->middleware('auth:admin');
        
    }

}

 

Model 수정

app/Models/Admin.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\UserInterface;

class Admin extends Authenticatable implements UserInterface
{
    use HasFactory;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'type'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',

    ];

    protected function getAdminType($type)
    {
        if($this->type === $type)
            return true;
        else
            return false;
    }

    public function isSuperAdministrator()
    {
        return $this->getAdminType('super_admin');
    }
    public function isAdministrator()
    {
        return $this->getAdminType('admin');
    }
    public function isModerator()
    {
        return $this->getAdminType('moderator');
    }
    public function isEditor()
    {
        return $this->getAdminType('editor');
    }

    public function isAdmin()
    {
        return true;
    }

    public function isAccessibleAdmins()
    {
        if ($this->isSuperAdministrator() || 
            $this->isAdministrator() ) {
            return true;
        }
    }

    public function isAccessibleUsers()
    {
        if ($this->isSuperAdministrator() || 
            $this->isAdministrator() ||
            $this->isModerator() ) {
            return true;
        }
    }

}

 

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use App\Models\UserInterface;

class User extends Authenticatable implements UserInterface
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];


    public function isAdmin()
    {
        return false;
    }

    public function isAccessibleAdmins()
    {
        return false;
    }

    public function isAccessibleUsers()
    {
        return false;
    }
}

app/Models/UserInterface.php

<?php

namespace App\Models;

interface UserInterface
{
    public function isAdmin();

    public function isAccessibleAdmins();

    public function isAccessibleUsers();

}

FormRequest 수정 

app/Http/Requests/StoreAdminRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreAdminRequest extends FormRequest
{
    protected $redirectRoute = 'admin.admins.create';
    
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required',
            'password' => 'required',
        ];
    }
}

app/Http/Requests/UpdateAdminRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateAdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required',
        ];
    }
}

User 모델의 app/Http/Requests/UpdateUserRequest.php 와 app/Http/Requests/StoreUserRequest.php 도 Admin 과 거의 비슷하니 내용파악하면서 수정하면 됩니다.

 

 

Policy 수정

app/Policies/AdminPolicy.php

현재 설정으로는 Admin 메뉴에 접근가능한 타입은 super admin 과 admin 으로 제한합니다.

<?php

namespace App\Policies;

use App\Models\Admin;
use App\Models\UserInterface;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class AdminPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\UserInterface  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function viewAny(UserInterface $admin)
    {
        if ($admin->isAccessibleAdmins()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You Are Not Allowed to Access This Page.' , 403);
        
        // if ($admin->isAccessibleAdmins()) {
        //     //            return true;
        //     return Response::allow();
        // }
        // Response::deny('You Are Not Allowed to Access This Page.');
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\Admin  $createdAdmin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(UserInterface $admin, Admin $createdAdmin)
    {
        if ($admin->isAccessibleAdmins()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('Sorry, You Are Not Allowed to Access This Page.' , 403);
        // return $admin->id === $createdAdmin->user_id
        //             ? Response::allow()
        //             : Response::deny('You Are Not Allowed to Access This Page.');
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\UserInterface  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function create(UserInterface  $admin)
    {
        if ($admin->isAccessibleAdmins()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('Sorry, You Are Not Allowed to Access This Page.');
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\UserInterface  $user
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(UserInterface  $admin, Admin $createdAdmin)
    {
        if ($admin->isAccessibleAdmins()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You Are Not Allowed to Access This Page.');
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(UserInterface  $admin, Admin $createdAdmin)
    {
        if ($admin->isAccessibleAdmins()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You Are Not Allowed to Access This Page.');
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function restore(UserInterface  $admin, Admin $createdAdmin)
    {
        //
    }

    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\Admin  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function forceDelete(UserInterface  $admin, Admin $createdAdmin)
    {
        //
    }
}

app/Policies/UserPolicy.php

editor 타입을 제외한 나머지 관리자 타입은 다 회원 메뉴에 접근 가능하게 했습니다.

이부분을 수정하여 접근가능 여부를 변경하면 됩니다.

<?php

namespace App\Policies;

use App\Models\User;
use App\Models\UserInterface;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\Models\UserInterface  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function viewAny(UserInterface $admin)
    {
        if ($admin->isAccessibleUsers()) {
            return true;
        }
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\Models\UserInterface  $user
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function view(UserInterface $admin, User $user)
    {
        if ($admin->isAccessibleUsers()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You do not permitted this page.' , 403);
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\UserInterface  $admin
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function create(UserInterface  $admin)
    {
        if ($admin->isAccessibleUsers()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You do not permitted this page.' , 403);
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function update(UserInterface  $admin, User $user)
    {
        if ($admin->isAccessibleUsers()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You do not permitted this page.');
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function delete(UserInterface  $admin, User $user)
    {
        if ($admin->isAccessibleUsers()) {
            //            return true;
            return Response::allow();
        }
        return Response::deny('You do not permitted this page.');
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function restore(UserInterface  $admin, User $user)
    {
        //
    }

    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \App\Models\UserInterface  $admin
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool
     */
    public function forceDelete(UserInterface  $admin, User $user)
    {
        //
    }
}

AuthServiceProvider.php 수정

위에서 설정한 두 Policy 를 매핑

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
//        'App\Models\Admin' => 'App\Policies\AdminPolicy',
        Admin::class => AdminPolicy::class,
        User::class => UserPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}

뷰파일들 작성

resources/views/admin/admins/index.blade.php

@extends('adminlte::page')

@section('title', 'Dashboard')

@section('content_header')
    <h1>관리자 어카운트 관리화면</h1>
@stop

@section('content')
@if(Session::has('success'))
<div class="alert alert-success" role="alert">
    {{ Session::get('success') }}
    @php
        Session::forget('success');
    @endphp
</div>

@endif


<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">관리자 리스트</h3>
                <div class=" float-right">
                    <div class="btn-group">
                        <a class="btn btn-primary btn-sm" style="color:white" href="{{ route('admin.admins.create') }}"><i class="fas fa-plus"></i> Create</a>
                    </div>
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>번호</th>
                        <th>이름</th>
                        <th>email</th>
                        <th>관리자 타입</th>
                        <th>등록일</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($models as $model)
                    <tr>
                        <td>{{ $model->id }}</td>
                        <td>{{ $model->name }}</td>
                        <td>{{ $model->email }}</td>
                        <td>{{ config('app_admin.admin_types')[$model->type] }}</td>
                        <td>{{ $model->created_at }}</td>
                        <td>
                            <a class="btn btn-success btn-sm " style="color:white" href="{{ route('admin.admins.edit', ['admin' => $model]) }}"><i class="fas fa-pencil-alt"></i> 수정</a>
                            <a class="btn btn-primary btn-sm " style="color:white" href="{{ route('admin.admins.show' , ['admin'=>$model])}}"><i class="fas fa-eye"></i> 보기</a>
                            <button type="button" class="btn btn-danger btn-sm " data-toggle="modal" data-target="#modal-delete-{{ $model->id }}">삭제</button>
                        </td>
                        @include('admin.admins.__delete_modal')
                    </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
            <!-- /.card-body -->
            <div class="card-footer clearfix">
                {{ $models->links('pagination::bootstrap-4') }}
            </div>
        </div>
        <!-- /.card -->
    </div>
</div>
@stop

@section('footer')
@stop

@section('css')
<link rel="stylesheet" href="/css/admin_custom.css">
@stop

@section('js')
<script> console.log('Hi!'); </script>
@stop

 

resources/views/admin/admins/show.blade.php

@extends('adminlte::page')

@section('title', 'Dashboard')

@section('content_header')
    <h1>관리자 어카운트 관리화면</h1>
@stop

@section('content')

@if(Session::has('success'))
<div class="alert alert-success" role="alert">
    {{ Session::get('success') }}
    @php
        Session::forget('success');
    @endphp
</div>
@endif

<div class="row">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">관리자 어카운트 상세정보</h3>

            </div>
            <!-- /.card-header -->
            <div class="card-body">
                <div class="form-group">
                    <label for="name">이름</label>
                    <div class="form-control border-0" >{{ $model->name }}</div>
                </div>
                <div class="form-group">
                    <label for="name">email</label>
                    <div class="form-control border-0" >{{ $model->email }}</div>
                </div>
                <div class="form-group">
                    <label for="name">Type</label>
                    <div class="form-control border-0 " style="background-color:gray" >{{ config('app_admin.admin_types')[$model->type] }}</div>
                </div>
                <div class="form-group">
                    <label for="name">Updated</label>
                    <div class="form-control border-0" >{{ $model->updated_at }}</div>
                </div>
                <div class="form-group">
                    <label for="name">Registered</label>
                    <div class="form-control border-0" >{{ $model->created_at }}</div>
                </div>
            </div>
            <div class="card-footer ">
                <a class="btn btn-primary btn-sm " style="color:white" href="{{ route('admin.admins.edit', ['admin' => $model]) }}"><i class="fas fa-pencil-alt"></i> 수정</a>
                <a class="btn btn-primary btn-sm " style="color:white" href="{{ route('admin.admins.index') }}"> 리스트</a>
                <button type="button" class="btn btn-danger btn-sm " data-toggle="modal" data-target="#modal-delete-{{ $model->id }}">
                        삭제
                </button>
            </div>
            @include('admin.admins.__delete_modal')
        </div>
        <!-- /.card -->
    </div>
</div>
@stop

@section('footer')

@stop

@section('css')
    <link rel="stylesheet" href="/css/admin_custom.css">
@stop

@section('js')
    <script> console.log('Hi!'); </script>
@stop

 

resources/views/admin/admins/create.blade.php

@extends('adminlte::page')

@section('title', 'Dashboard')

@section('content_header')
    <h1>관리자 어카운트 관리화면</h1>
@stop

@section('content')
    {{-- <div class="alert alert-success" role="alert">
    Welcome to admin users
    </div> --}}

    <div class="row">
        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
                <h3 class="card-title">관리자 어카운트 작성</h3>
    
            </div>

            <form method="POST" action="{{ route('admin.admins.store') }}">
                @method('POST')
                @csrf
            <div class="card-body">
                <div class="form-group">
                    <label for="name">이름</label>
                    <input id="name" name="name" type="text" 
                            class="@error('name') is-invalid @enderror form-control">

                    @error('name')
                        <div class="alert alert-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="form-group">
                    <label for="name">email</label>
                    <input id="email" name="email" type="text" 
                            class="@error('email') is-invalid @enderror form-control">

                    @error('email')
                        <div class="alert alert-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="form-group">
                    <label for="name">password</label>
                    <input id="password" name="password" type="password" 
                            class="@error('password') is-invalid @enderror form-control">

                    @error('password')
                        <div class="alert alert-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="form-group">
                    <label for="name">Type</label>
                    @foreach ($types as $key => $typeName)
                        <div class="form-check">
                            <input type="radio"
                            name="type"
                            value="{{ $key }}"
                            @if ($key == 'editor')
                                checked
                            @endif
                            >
                            <label for="type">{{ $typeName }}</label>
                        </div>
                    @endforeach

                </div>

            </div>
            <div class="card-footer clearfix">
                <input id="submit" type="submit" class="btn btn-primary btn-sm" value="등록">
                <a class="btn btn-primary btn-sm " style="color:white" href="{{ route('admin.admins.index') }}">리스트</a>
            </div>
            </form>
            <!-- form end -->

          </div>
          <!-- /.card -->
        </div>
    </div>

@stop

@section('footer')

@stop

@section('css')
    <link rel="stylesheet" href="/css/admin_custom.css">
@stop

@section('js')
    <script> console.log('Hi!'); </script>
@stop

 

resources/views/admin/admins/edit.blade.php

@extends('adminlte::page')

@section('title', 'Dashboard')

@section('content_header')
    <h1>관리자 어카운트 관리화면</h1>
@stop

@section('content')
<div class="row">
    <div class="col-md-6">
        <div class="card">
        <div class="card-header">
            <h3 class="card-title">관리자 어카운트 수정</h3>

        </div>

        <form method="POST" action="{{ route('admin.admins.update' , ['admin' => $model]) }}">
            @method('PUT')
            @csrf
        <div class="card-body">
            <div class="form-group">
                <label for="name">이름</label>
                <input id="name" name="name" type="text"  value="{{ $model->name }}"
                        class="@error('name') is-invalid @enderror form-control">

                @error('name')
                    <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="form-group">
                <label for="name">email</label>
                <input id="email" name="email" type="text" value="{{ $model->email }}"
                        class="@error('email') is-invalid @enderror form-control">

                @error('email')
                    <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            {{-- <div class="form-group">
                <label for="name">password</label>
                <input id="password" name="password" type="password" 
                        class="@error('password') is-invalid @enderror form-control">

                @error('password')
                    <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div> --}}
            <div class="form-group">
                <label for="name">Type</label>
                @foreach ($types as $key => $typeName)
                    <div class="form-check">
                        <input type="radio"
                        name="type"
                        value="{{ $key }}"
                        @if ($key == $model->type)
                            checked
                        @endif
                        >
                        <label for="type">{{ $typeName }}</label>
                    </div>
                @endforeach

            </div>

        </div>
        <div class="card-footer clearfix">
            <input id="submit" type="submit" class="btn btn-primary" value="수정">
        </div>
        </form>
        <!-- form end -->

        </div>
        <!-- /.card -->
    </div>
</div>

@stop

@section('footer')

@stop

@section('css')
    <link rel="stylesheet" href="/css/admin_custom.css">
@stop

@section('js')
    <script> console.log('Hi!'); </script>
@stop

 

resources/views/admin/admins/__delete_modal.blade.php

<!-- Modal -->
<div class="modal fade" 
    id="modal-delete-{{ $model->id }}" 
    tabindex="-1" 
    role="dialog" 
    aria-labelledby="modalDeleteLabel{{ $model->id }}" 
    aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
        <h5 class="modal-title" id="modalDeleteLabel{{ $model->id }}">삭제 확인</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        </div>
        <div class="modal-body">
            {{ $model->name }} 를 정말로 삭제합니까?
        </div>
        <div class="modal-footer">
            <form method="POST" 
                action="{{ route('admin.admins.destroy' , ['admin' => $model]) }}">
                @method('DELETE')
                @csrf
            <button type="button" 
                    class="btn btn-secondary btn-sm" 
                    data-dismiss="modal">Close</button>
            <input id="submit" type="submit" class="btn btn-danger btn-sm" value="삭제">
            </form>
        </div>
    </div>
    </div>
</div>

 

resources/views/admin/users/ 안의 뷰파일들도 admins 안의 파일들과 거의 같습니다.

route 이름과 model의 property 등이 조금 다르니 이부분은 admin 부분 참고해서 직접수정해보시기 바랍니다.

 

 

여기까지 하면 기본적인 관리화면에서 관리자 어카운트 / 회원 어카운트 관리 화면의 일반적인 기능들은 다 만들어진셈입니다.

 

 

반응형
SMALL

댓글