edit($id)
:edit($id)
thường dùng để hiển thị màn hình bao gồm form và các ô nhập liệu (input).Lưu
app\Http\Controllers\Backend\SanphamController.php
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // Sử dụng Eloquent Model để truy vấn dữ liệu $sp = SanPham::where("sp_ma", $id)->first(); $ds_loai = Loai::all(); // Đường dẫn đến view được quy định như sau: <FolderName>.<ViewName> // Mặc định đường dẫn gốc của method view() là thư mục `resources/views` // Hiển thị view `backend.sanpham.edit` return view('sanpham.edit') // với dữ liệu truyền từ Controller qua View, được đặt tên là `sp` và `danhsachloai` ->with('sp', $sp) ->with('danhsachloai', $ds_loai); }
edit.blade.php
resources/views/sanpham
resources/views/sanpham/edit.blade.php
@extends('backend.layouts.master') @section('title') Hiệu chỉnh sản phẩm @endsection @section('custom-css') <!-- Các css dành cho thư viện bootstrap-fileinput --> <link href="{{ asset('vendor/bootstrap-fileinput/css/fileinput.css') }}" media="all" rel="stylesheet" type="text/css"/> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" crossorigin="anonymous"> <link href="{{ asset('vendor/bootstrap-fileinput/themes/explorer-fas/theme.css') }}" media="all" rel="stylesheet" type="text/css"/> @endsection @section('content') @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form method="post" action="{{ route('admin.sanpham.update', ['id' => $sp->sp_ma]) }}" enctype="multipart/form-data"> <input type="hidden" name="_method" value="PUT" /> {{ csrf_field() }} <div class="form-group"> <label for="l_ma">Loại sản phẩm</label> <select name="l_ma" class="form-control"> @foreach($danhsachloai as $loai) @if($loai->l_ma == $sp->l_ma) <option value="{{ $loai->l_ma }}" selected>{{ $loai->l_ten }}</option> @else <option value="{{ $loai->l_ma }}">{{ $loai->l_ten }}</option> @endif @endforeach </select> </div> <div class="form-group"> <label for="sp_ten">Tên sản phẩm</label> <input type="text" class="form-control" id="sp_ten" name="sp_ten" value="{{ old('sp_ten', $sp->sp_ten) }}"> </div> <div class="form-group"> <label for="sp_giaGoc">Giá gốc</label> <input type="text" class="form-control" id="sp_giaGoc" name="sp_giaGoc" value="{{ old('sp_giaGoc', $sp->sp_giaGoc) }}"> </div> <div class="form-group"> <label for="sp_giaGoc">Giá bán</label> <input type="text" class="form-control" id="sp_giaBan" name="sp_giaBan" value="{{ old('sp_giaBan', $sp->sp_giaBan) }}"> </div> <div class="form-group"> <div class="file-loading"> <label>Hình đại diện</label> <input id="sp_hinh" type="file" name="sp_hinh"> </div> </div> <div class="form-group"> <label for="sp_thongTin">Thông tin</label> <input type="text" class="form-control" id="sp_thongTin" name="sp_thongTin" value="{{ old('sp_thongTin', $sp->sp_thongTin) }}"> </div> <div class="form-group"> <label for="sp_danhGia">Đánh giá</label> <input type="text" class="form-control" id="sp_danhGia" name="sp_danhGia" value="{{ old('sp_danhGia', $sp->sp_danhGia) }}"> </div> <div class="form-group"> <label for="sp_taoMoi">Ngày tạo mới</label> <input type="text" class="form-control" id="sp_taoMoi" name="sp_taoMoi" value="{{ old('sp_taoMoi', $sp->sp_taoMoi) }}" data-mask-datetime> </div> <div class="form-group"> <label for="sp_capNhat">Ngày cập nhật</label> <input type="text" class="form-control" id="sp_capNhat" name="sp_capNhat" value="{{ old('sp_capNhat', $sp->sp_capNhat) }}" data-mask-datetime> </div> <div class="form-group"> <label for="sp_trangThai">Trạng thái</label> <select name="sp_trangThai" class="form-control"> <option value="1" {{ old('sp_trangThai', $sp->sp_trangThai) == 1 ? "selected" : "" }}>Khóa</option> <option value="2" {{ old('sp_trangThai', $sp->sp_trangThai) == 2 ? "selected" : "" }}>Khả dụng</option> </select> </div> <button type="submit" class="btn btn-primary">Lưu</button> </form> @endsection @section('custom-scripts') <!-- Các script dành cho thư viện bootstrap-fileinput --> <script src="{{ asset('vendor/bootstrap-fileinput/js/plugins/sortable.js') }}" type="text/javascript"></script> <script src="{{ asset('vendor/bootstrap-fileinput/js/fileinput.js') }}" type="text/javascript"></script> <script src="{{ asset('vendor/bootstrap-fileinput/js/locales/fr.js') }}" type="text/javascript"></script> <script src="{{ asset('vendor/bootstrap-fileinput/themes/fas/theme.js') }}" type="text/javascript"></script> <script src="{{ asset('vendor/bootstrap-fileinput/themes/explorer-fas/theme.js') }}" type="text/javascript"></script> <script> $(document).ready(function() { $("#sp_hinh").fileinput({ theme: 'fas', showUpload: false, showCaption: false, browseClass: "btn btn-primary btn-lg", fileType: "any", append: false, showRemove: false, autoReplace: true, previewFileIcon: "<i class='glyphicon glyphicon-king'></i>", overwriteInitial: false, initialPreviewShowDelete: false, initialPreviewAsData: true, initialPreview: [ "{{ asset('storage/photos/' . $sp->sp_hinh) }}" ], initialPreviewConfig: [ { caption: "{{ $sp->sp_hinh }}", size: {{ Storage::exists('public/photos/' . $sp->sp_hinh) ? Storage::size('public/photos/' . $sp->sp_hinh) : 0 }}, width: "120px", url: "{$url}", key: 1 }, ] }); }); </script> <!-- Các script dành cho thư viện Mặt nạ nhập liệu InputMask --> <script src="{{ asset('vendor/input-mask/jquery.inputmask.min.js') }}"></script> <script src="{{ asset('vendor/input-mask/bindings/inputmask.binding.js') }}"></script> <script> $(document).ready(function() { // Gắn mặt nạ nhập liệu cho các ô nhập liệu Giá gốc $('#sp_giaGoc').inputmask({ alias: 'currency', positionCaretOnClick: "radixFocus", radixPoint: ".", _radixDance: true, numericInput: true, groupSeparator: ",", suffix: ' vnđ', min: 0, // 0 ngàn max: 100000000, // 1 trăm triệu autoUnmask: true, removeMaskOnSubmit: true, unmaskAsNumber: true, inputtype: 'text', placeholder: "0", definitions: { "0": { validator: "[0-9\uFF11-\uFF19]" } } }); // Gắn mặt nạ nhập liệu cho các ô nhập liệu Giá bán $('#sp_giaBan').inputmask({ alias: 'currency', positionCaretOnClick: "radixFocus", radixPoint: ".", _radixDance: true, numericInput: true, groupSeparator: ",", suffix: ' vnđ', min: 0, // 0 ngàn max: 100000000, // 1 trăm triệu autoUnmask: true, removeMaskOnSubmit: true, unmaskAsNumber: true, inputtype: 'text', placeholder: "0", definitions: { "0": { validator: "[0-9\uFF11-\uFF19]" } } }); // Gắn mặt nạ nhập liệu cho các ô nhập liệu Ngày tạo mới $('#sp_taoMoi').inputmask({ alias: 'datetime', inputFormat: 'yyyy-mm-dd' // Định dạng Năm-Tháng-Ngày }); // Gắn mặt nạ nhập liệu cho các ô nhập liệu Ngày cập nhật $('#sp_capNhat').inputmask({ alias: 'datetime', inputFormat: 'yyyy-mm-dd' // Định dạng Năm-Tháng-Ngày }); }); </script> @endsection
update($id)
:update($id)
thường dùng để thực thi câu lệnh UPDATE dữ liệu vào database.index
app\Http\Controllers\Backend\SanphamController.php
use Storage; use Session; /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // Bổ sung ràng buộc Validate $validation = $request->validate([ 'sp_hinh' => 'file|image|mimes:jpeg,png,gif,webp|max:2048', ]); // Tìm object Sản phẩm theo khóa chính $sp = SanPham::where("sp_ma", $id)->first(); $sp->sp_ten = $request->sp_ten; $sp->sp_giaGoc = $request->sp_giaGoc; $sp->sp_giaBan = $request->sp_giaBan; $sp->sp_thongTin = $request->sp_thongTin; $sp->sp_danhGia = $request->sp_danhGia; $sp->sp_taoMoi = $request->sp_taoMoi; $sp->sp_capNhat = $request->sp_capNhat; $sp->sp_trangThai = $request->sp_trangThai; $sp->l_ma = $request->l_ma; // Kiểm tra xem người dùng có upload hình ảnh Đại diện hay không? if($request->hasFile('sp_hinh')) { // Xóa hình cũ để tránh rác Storage::delete('public/photos/' . $sp->sp_hinh); // Upload hình mới // Lưu tên hình vào column sp_hinh $file = $request->sp_hinh; $sp->sp_hinh = $file->getClientOriginalName(); // Chép file vào thư mục "photos" $fileSaved = $file->storeAs('public/photos', $sp->sp_hinh); } $sp->save(); // Hiển thị câu thông báo 1 lần (Flash session) Session::flash('alert-info', 'Cập nhật thành công ^^~!!!'); // Điều hướng về trang index return redirect()->route('admin.sanpham.index'); }
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!