สมมุติว่าเราจะดึงข้อมูบ Users มาแสดง และแก้ไข โดยสร้าง View ขึ้นมาและให้ระบบแสดงข้อมูล และฟอร์มแก้ไขอัตโนมัติ
ใน Model ในไฟล์ app/User.php ให้ทำการเพิ่มข้อมูลตามโค๊ดด้านล่าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
<?php namespace App; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'phone', 'language', 'timezone', 'is_admin', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', 'is_admin' => 'boolean', ]; protected $dates = ['deleted_at']; /** * Check user is admin? * * @return 1 or null */ public function isAdmin() { return $this->is_admin; // this looks for an is_admin column in your users table } /** * กำหนดรายละเอียดของฟิวด์ เพื่อนำไปสร้างหน้า Input Form อัตโนมัติ * * @return array */ // public function getColumnsDetail() { $field['name'] = ['type' => 'text', 'label' => 'Name', 'value' => '', 'required' => true, 'class' => 'form-control', 'id' => '','readonly'=>'']; $field['email'] = ['type' => 'text', 'label' => 'Email', 'value' => '', 'required' => true, 'class' => 'form-control', 'id' => '', 'readonly'=>'']; $field['phone'] = ['type' => 'number', 'label' => 'Phone', 'value' => '', 'required' => '', 'class' => 'form-control', 'id' => '','readonly'=>'']; $field['language'] = ['type' => 'select', 'label' => 'Language', 'value' => config('app.locales'), 'required' => '', 'class' => 'form-control', 'id' => '','readonly'=>'']; $field['timezone'] = ['type' => 'select', 'label' => 'Timezone', 'value' => $this->getTimezone(), 'required' => '', 'class' => 'form-control', 'id' => '','readonly'=>'']; //ฟิวด์นี้เห็นเฉพาะ Admin if($this->isAdmin()){ $field['is_admin'] = ['type' => 'checkbox','label' => 'Is Admin','value' => 1,'required' => '','class' => 'form-check-input','id' => '','readonly'=>'']; } return $field; } /** * กำหนดฟิวด์ที่จะดึงไปแสดงในหน้า DetailView,EditView * * @return array */ // public function getColumns() { $columns = $this->fillable; foreach($this->hidden as $value){ //ไม่ดึงฟิวด์ที่อยู่ใน protected $hidden มาแสดงด้วย $key = array_search($value,$columns); if (false !== $key){ unset($columns[$key]); } } return $columns; } public function getTimezone(){ $timezones = timezone_identifiers_list(); $return = []; foreach($timezones as $value){ $return[$value] = $value; } return $return; } } |
ใน app/Http/Controllers/UserController.php ในส่วนของการแสดงข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
... /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show(Request $request,$id) { // get the user $user = User::findOrFail($id); //หากเป็น Admin หรือ User id นี้ มีสิทธ์แก้ไขให้แสดงปุ่มแก้ไข if($request->user()->isAdmin()||$request->user()->id==$id){ //edit button $data['btn_edit'] = ['url'=> route('user.edit', [$id]),'class'=> 'btn btn-default btn-sm','value'=> 'Edit',]; } // $data['title'] = $user->name; $data['id'] = $user->id; //get data $columns = $request->user()->getColumns(); $columnsDetail = $request->user()->getColumnsDetail(); foreach($columns as $value){ if(isset($columnsDetail[$value])){ $fielddata[$columnsDetail[$value]['label']] = $user->$value; }else{ $fielddata[$value] = $user->$value; } } $data['block']['Profile Info'] = $fielddata; // show the view and pass the user to it return View('detailview') ->with('data', $data); } ... |
ในหน้า resources/views/detailview.blade.php จะทำการดึงข้อมูลมาแสดงอัตโนมัติ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
@extends('layouts.main') @section('content') <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">{{ $data['title'] ?? '' }}</h1> </div><!-- /.col --> <div class="col-sm-6 text-right "> @isset($data['btn_edit']) <a href="{{ $data['btn_edit']['url'] }}" class="{{ $data['btn_edit']['class'] }}" type="button">{{ $data['btn_edit']['value'] }}</a> @endisset </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <div class="content"> <div class="container-fluid"> @foreach ($data['block'] as $header=>$detail) <div class="card"> <div class="card-header"> <h3 class="card-title">{{ $header }}</h3> <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="collapse" data-toggle="tooltip" title="Collapse"> <i class="fas fa-minus"></i></button> <!--<button type="button" class="btn btn-tool" data-card-widget="remove" data-toggle="tooltip" title="Remove"> <i class="fas fa-times"></i></button>--> </div> </div> <div class="card-body p-2" > <div class="row"> @foreach ($detail as $label=>$value) <div class="col-12 col-md-6"> <div class="row"> <div class="col-4 text-right"><strong>{{ $label }}</strong></div> <div class="col-8 text-left">{{$value}}</div> </div> </div> @endforeach </div> </div> </div> </div><!-- /.container-fluid --> </div> <!-- /.content --> @endforeach @endsection |
ใน app/Http/Controllers/UserController.php ในส่วนของการแก้ไข
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
... /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit(Request $request,$id) { if(!$request->user()->isAdmin()){ if($request->user()->id!=$id){ return View('errors.custom') ->with('message', trans('error.not_permission')); } } // get the user $user = User::findOrFail($id); //back button $data['btn_back'] = ['url'=> route('user.show', [$id]),'class'=> 'btn btn-default btn-sm','value'=> 'Back',]; // $data['title'] = $user->name; $data['id'] = $user->id; //get data $columns = $request->user()->getColumns(); $columnsDetail = $request->user()->getColumnsDetail(); foreach($columns as $value){ if(isset($columnsDetail[$value])){ if($columnsDetail[$value]['type']=='select'||$columnsDetail[$value]['type']=='radio'||$columnsDetail[$value]['type']=='checkbox'){ $listsvalue = $columnsDetail[$value]['value'] ? $columnsDetail[$value]['value'] : []; $fielddata[$value]['value'] = ['lists'=>$listsvalue,'value'=>$user->$value]; }else{ $fielddata[$value]['value'] = $user->$value; } $fielddata[$value]['type'] = $columnsDetail[$value]['type']; $fielddata[$value]['label'] = $columnsDetail[$value]['label']; $fielddata[$value]['required'] = $columnsDetail[$value]['required']; $fielddata[$value]['class'] = $columnsDetail[$value]['class']; $fielddata[$value]['id'] = $columnsDetail[$value]['id']; $fielddata[$value]['readonly'] = $columnsDetail[$value]['readonly']; } } $data['block']['Profile Info'] = $fielddata; // show the edit form and pass the user return View('editview') ->with('data', $data); } ... |
ในหน้า resources/views/editview.blade.php จะทำการดึงข้อมูลและ input form มาแสดงอัตโนมัติ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
@extends('layouts.main') @section('content') <!-- Content Header (Page header) --> <div class="content-header"> <div class="container"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">{{ $data['title'] ?? '' }}</h1> </div><!-- /.col --> <div class="col-sm-6 text-right "> @isset($data['btn_edit']) <a href="{{ $data['btn_edit']['url'] }}" class="{{ $data['btn_edit']['class'] }}" type="button">{{ $data['btn_edit']['value'] }}</a> @endisset @isset($data['btn_back']) <a href="{{ $data['btn_back']['url'] }}" class="{{ $data['btn_back']['class'] }}" type="button">{{ $data['btn_back']['value'] }}</a> @endisset </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <div class="content"> <div class="container"> <form action="{{ route('user.update', $data['id']) }}" method="POST"> @csrf @method('PUT') <!--Validate Error Message--> @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <!-- will be used to show any messages --> @if (Session::has('message')) <div class="alert alert-success"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><i class="fa fa-info-circle"></i> {{ Session::get('message') }}</div> @endif @foreach ($data['block'] as $header=>$detail) <div class="card"> <div class="card-header"> <h3 class="card-title">{{ $header }}</h3> <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="collapse" data-toggle="tooltip" title="Collapse"> <i class="fas fa-minus"></i></button> <!--<button type="button" class="btn btn-tool" data-card-widget="remove" data-toggle="tooltip" title="Remove"> <i class="fas fa-times"></i></button>--> </div> </div> <div class="card-body p-2" > <div class="row"> @foreach ($detail as $fieldname=>$value) <div class="col-12 col-md-6 mb-3"> <div class="form-row"> @switch($value['type']) @case("text") @case("hidden") @case("number") @case("email") @case("date") @case("datetime-local") @case("month") @case("time") @case("week") @case("password") @case("url") @case("tel") @case("file") <div class="col-12"> <div class="form-group"> <label> {{ $value['label'] }} @if($value['required']=='required') <span class="text-danger">*</span> @endif </label> @if($value['readonly']=='readonly') {{ $value['value'] }} @else <input type="{{ $value['type'] }}" value="{{ $value['value'] }}" class="{{ $value['class'] }} @error($fieldname) is-invalid @enderror" id="{{ $value['id'] }}" name="{{ $fieldname }}"> @endif @error($fieldname) <div class="alert alert-danger">{{ $message }}</div> @enderror </div> </div> @break @case("select") <div class="col-12"> <div class="form-group"> <label> {{ $value['label'] }} @if($value['required']=='required') <span class="text-danger">*</span> @endif </label> @if($value['readonly']=='readonly') {{ $value['value'] }} @else <select class="{{ $value['class'] }} @error($fieldname) is-invalid @enderror" id="{{ $value['id'] }}" name="{{ $fieldname }}"> @foreach ($value['value']['lists'] as $index=>$list) <option value="{{$index}}" @if ($value['value']['value']==$index) selected="selected" @endif>{{$list}}</option> @endforeach </select> @endif @error($fieldname) <div class="alert alert-danger">{{ $message }}</div> @enderror </div> </div> @break @case("checkbox") <div class="col-12"> <div class="form-group"> <label>{{ $value['label'] }} @if($value['required']=='required') <span class="text-danger">*</span> @endif </label> @if($value['readonly']=='readonly') {{ $value['value'] }} @else <div class="form-check"> <input type="checkbox" value="{{$value['value']['lists']}}" class="{{ $value['class'] }} @error($fieldname) is-invalid @enderror" id="{{ $value['id'] }}" name="{{ $fieldname }}" @if($value['value']['lists']==$value['value']['value']) checked @endif > </div> @endif @error($fieldname) <div class="alert alert-danger">{{ $message }}</div> @enderror </div> </div> @break @case("radio") <div class="col-12"> <div class="form-group"> <label> {{ $value['label'] }} @if($value['required']=='required') <span class="text-danger">*</span> @endif </label> @foreach ($value['value']['lists'] as $index=>$list) <div class="form-check"> <input type="radio" value="{{$index}}" class="{{ $value['class'] }} @error($fieldname) is-invalid @enderror" id="{{ $value['id'] }}" name="{{ $fieldname }}" @if($index==$value['value']['value']) checked @endif> <label class="form-check-label"> {{$list}} </label> </div> @endforeach </div> </div> @break @default <div class="col-12"> <div class="form-group"> <label> {{ $value['label'] }} @if($value['required']=='required') <span class="text-danger">*</span> @endif </label> @if($value['readonly']=='readonly') {{ $value['value'] }} @else <input type="{{ $value['type'] }}" value="{{ $value['value'] }}" class="{{ $value['class'] }} @error($fieldname) is-invalid @enderror" id="{{ $value['id'] }}" name="{{ $fieldname }}"> @endif @error($fieldname) <div class="alert alert-danger">{{ $message }}</div> @enderror </div> </div> @endswitch </div> </div> @endforeach </div> </div> </div> @endforeach <div class="row"> <div class="col-12 mb-1 text-center"> <button type="submit" class="btn btn-primary">Save</button> <a href="{{ $data['btn_back']['url'] }}" class="btn btn-default">Cancel</a> </div> </div> </form> </div><!-- /.container-fluid --> </div> <!-- /.content --> @endsection |
ดูเกี่ยวกับ Auto generate forms เพิ่มเติม
ป้ายกำกับ:laravel, ฟอร์มแก้ไขอัตโนมัติ, แสดงข้อมูลอัตโนมัติ