1. xyz/resources/views/mgmt/admin/update_form.blade.php 을 작성한다.

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 수정</h4>
</div>
<div class="modal-body">
  <form name="update_form" action="/mgmt/admin/update" method="post">
    @csrf
    <div class="form-group">
      <label>아이디</label>
      <input type="text" name="userid" class="form-control" readonly required pattern="[a-zA-Z0-9]+" value="{{ $admin->userid }}"/>
      <input type="hidden" name="id" class="form-control" value="{{ $admin->sno }}" />
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required value="{{ $admin->nick }}"/>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 수정" />
    </div>
  </form>
</div>

 

2. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
    // 관리자 수정 폼
    // /mgnt/admin/update_form/{id}
    public function update_form ($id) {
        // $t_admin = DB::select('CALL getAdmin(?)', [$id]);
        // $admin = $t_admin[0];
        $admin = collect(DB::select('CALL getAdmin(?)', [$id]))->first();
        return view('mgmt.admin.update_form', ['admin' => $admin]);
    }
 
...

 

3. xyz/routes/web.php 에 다음을 추가한다.

    Route::get('admin/update_form/{id}', [Admin::class, 'update_form']);

 

4. 이제, 수정 버튼을 클릭하면 수정할 수 있는 폼이 나타나게 될 것이다. 마지막으로 실제로 수정을 처리하는 루틴을 작성하고 라우터에 등록하자. 그전에 updateAdmin 이라는 저장 프로시저부터 만들어야겠지?

 

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 262
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE updateAdmin
    (i_sno INT,
     i_nick VARCHAR(255))
BEGIN
    UPDATE admins SET nick = i_nick WHERE sno = i_sno;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> CALL updateAdmin(1, 'nick101');
Query OK, 1 ROW affected (0.00 sec)
 
mysql> CALL listAdmins();
+-----+---------+-----------+---------+
| sno | userid  | password  | nick    |
+-----+---------+-----------+---------+
|   1 | testid1 | passwd101 | nick101 |
|   2 | testid2 | passwd2   | nick2   |
|   3 | testid3 | passwd3   | nick3   |
|   4 | testid4 | passwd4   | nick4   |
|   5 | testid5 | passwd5   | nick5   |
|   9 | testid6 | passwd106 | nick6   |
+-----+---------+-----------+---------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

5. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
    // 관리자 수정
    // /mgmt/admin/update
    public function update (Request $request) {
        $id   = $request->input('id');
        $nick = $request->input('nick');
 
        DB::update('CALL updateAdmin(?, ?)', [$id, $nick]);
        return redirect('/mgmt/admin');
    }
 
...

 

6. xyz/routes/web.php 에 다음을 추가하고, 수정 작업을 진행해보자.

    Route::post('admin/update', [Admin::class, 'update']);

+ Recent posts