PHP Laravel 6 – CRUD 기본 : 목록 , 수정 , 삭제 (4)

> 편집  TestCRUDController

publicfunctionedit($id)

    {

$test = TestCRUD::findOrFail($id);

returnview(‘test_view.modifyform’, compact(‘test’));

    }

>  view 파일 생성   modifyform.blade.php

@extends(‘layout’)

@section(‘content’)

<style>

.uper {

margin-top: 40px;

  }

</style>

<divclass=”card uper”>

<divclass=”card-header”>

    <h1>Modify Form for test</h1>

</div>

<divclass=”card-body”>

    @if ($errors->any())

<divclass=”alert alert-danger”>

<ul>

            @foreach ($errors->all() as $error)

<li>{{ $error }}</li>

            @endforeach

</ul>

</div><br/>

    @endif

<formmethod=”post”action=”{{ route(‘test_crud.update’, $test->id) }}”>

<divclass=”form-group”>

              @csrf

              @method(‘PATCH’)

<labelfor=”title”>Title:</label>

<inputtype=”text”class=”form-control”name=”title”value=”{{ $test->title }}”/>

</div>

<divclass=”form-group”>

<labelfor=”description”>Description :</label>

<inputtype=”text”class=”form-control”name=”description”value=”{{ $test->description }}”/>

</div>

<buttontype=”submit”class=”btn btn-primary”>Modify</button>

</form>

</div>

</div>

@endsection

> 편집 : index.blade.php

<tableclass=”table table-striped”>

<thead>

<tr>

<td></td>

<td>Title</td>

<td>description</td>

<tdcolspan=”3″>기능</td>

</tr>

</thead>

<tbody>

    @foreach($test as $column)

<tr>

<td>{{$column->id}}</td>

<td>{{$column->title}}</td>

<td>{{$column->description}}</td>

<td><ahref=”{{ route(‘test_crud.show’, $column->id)}}”class=”btn btn-primary”>확인</a></td>

<td><a href=”{{ route(‘test_crud.edit’, $column->id)}}” class=”btn btn-success”>수정</a></td>

<td></td>

</tr>

        @endforeach

</tbody>

</table>

> 확인  ( http://도메인/test_crud )

> 확인 : “수정” 버튼을 누르면 아래 화면과 같이 된다.

> 편집 위 화면의  “Modify”  버튼을 누르면 수정되어 목록으로 되거나 입력을 해야하는 값을 넣지 않으면 저장되지 않되록 한다.
편집 파일 TestCRUDController

publicfunctionupdate(Request$request, $id)

    {

$validatedData = $request->validate([

‘title’ => ‘required|max:100’,

‘description’ => ‘required’

        ]);

TestCRUD::whereId($id)->update($validatedData);

returnredirect(‘/test_crud’)->with(‘success’, ‘test is successfully updated’);

    }

>  실행 “Modify” 를 누르면 아래와 같이 실행된다.

삭제 ——————
> 편집  TestCRUDController

publicfunctiondestroy($id)

    {

$test = TestCRUD::findOrFail($id);

$test->delete();

returnredirect(‘/test_crud’)->with(‘success’, ‘test is successfully deleted’);

    }

> 편집 index.blade.php

<tableclass=”table table-striped”>

<thead>

<tr>

<td></td>

<td>Title</td>

<td>description</td>

<tdcolspan=”3″>기능</td>

</tr>

</thead>

<tbody>

    @foreach($test as $column)

<tr>

<td>{{$column->id}}</td>

<td>{{$column->title}}</td>

<td>{{$column->description}}</td>

<td><ahref=”{{ route(‘test_crud.show’, $column->id)}}”class=”btn btn-primary”>확인</a></td>

<td><ahref=”{{ route(‘test_crud.edit’, $column->id)}}”class=”btn btn-success”>수정</a></td>

<td>

<formaction=”{{ route(‘test_crud.destroy’, $column->id)}}”method=”post”>

                  @csrf

                  @method(‘DELETE’)

<buttonclass=”btn btn-danger”type=”submit”>삭제</button>

</form>

</td>

</tr>

        @endforeach

</tbody>

</table>

> 실행 (  http://도메인/test_crud )

> 실행  “삭제”  버튼을 누르면 아래와 같이 된다 

소스 : https://github.com/nzhongtting/Laravel_hsh_board

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다