<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.admin')
@section('title')
<title>Add Product</title>
@endsection
@section('css')
<link rel="stylesheet" href="{{ asset('admins/product/index/list.css') }}">
@endsection
@section('js')
<script src="{{asset('vendors/sweetAlert2/sweetalert2@11.js')}}"></script>
<script type="text/javascript" src="{{ asset('admins/main.js') }}"></script>
@endsection
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'product', 'key'=>'List']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('product.create') }}" class="btn btn-success float-right mr-2">add</a>
</div>
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tên sản phẩm</th>
<th scope="col">Giá</th>
<th scope="col">Hình ảnh</th>
<th scope="col">Danh mục</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($products as $productItem)
<tr>
<th scope="row">{{ $productItem->id }}</th>
<td>{{ $productItem->name}} </td>
<td>{{ number_format($productItem->price) }}</td>
<td>
<img class="product_image" src="{{$productItem->feature_image_path}}" alt="">
</td>
<td>{{ optional($productItem->category)->name }}</td>
<td>
<a href="{{ route('product.edit', ['id'=>$productItem->id]) }}"
class="btn btn-default">Edit</a>
<a href=""
data-url="{{ route('product.delete',['id'=>$productItem->id]) }}"
class="btn btn-danger action_delete">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-md-12">
{{ $products->links() }}
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Vào resources => admin => slider=> index.blade.php
<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.admin')
@section('title')
<title>Trang chủ</title>
@endsection
@section('css')
<link rel="stylesheet" href="{{ asset('admins/slider/index/index.css') }}">
@endsection
@section('js')
<script src="{{asset('vendors/sweetAlert2/sweetalert2@11.js')}}"></script>
<script type="text/javascript" src="{{ asset('admins/main.js') }}"></script>
@endsection
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'Slider', 'key'=>'Add']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('slider.create') }}" class="btn btn-success float-right mr-2">add</a>
</div>
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tên Slider</th>
<th scope="col">Description</th>
<th scope="col">Hình ảnh</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($sliders as $slider)
<tr>
<th scope="row">{{ $slider->id }}</th>
<td>{{ $slider->name }}</td>
<td>{{ $slider->description }}</td>
<td>
<img class="image_slider_150_100" src="{{ $slider->image_path }}" alt="">
</td>
<td>
<a href="{{ route('slider.edit',['id'=>$slider->id]) }}"
class="btn btn-default">Edit</a>
<a href=""
class="btn btn-danger action_delete"
data-url={{ route('slider.delete',['id'=>$slider->id]) }}>Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-md-12">
{{ $sliders->links() }}
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Vào Trait.php => tạo mới DeleteModelTrait.php
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Log;
trait DeleteModelTrait {
public function deleteModelTrait($id, $model)
{
try {
$model->find($id)->delete();
return response()->json([
'code' => 200,
'message' => 'success'
], 200);
}catch (\Exception $exception){
log::error('Message: ' .$exception->getMessage().' --- Line : '.$exception->getLine());
return response()->json([
'code' => 500,
'message' => 'fail'
], 500);
}
}
}
- Vào AdminSettingController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\AddSettingRequest;
use App\Setting;
use App\Traits\DeleteModelTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class AdminSettingController extends Controller
{
use DeleteModelTrait;
private $setting;
public function __construct(Setting $setting)
{
$this->setting = $setting;
}
public function index()
{
$settings = $this->setting->latest()->paginate(5);
return view('admin.setting.index',compact('settings'));
}
public function create(){
return view('admin.setting.add');
}
public function store(AddSettingRequest $request)
{
$this->setting->create([
'config_key'=>$request->config_key,
'config_value'=>$request->config_value,
'type'=>$request->type,
]);
return redirect()->route('settings.index');
}
public function edit($id)
{
$setting = $this->setting->find($id);
return view('admin.setting.edit',compact('setting'));
}
public function update(Request $request,$id)
{
$this->setting->find($id)->update([
'config_key'=>$request->config_key,
'config_value'=>$request->config_value
]);
return redirect()->route('settings.index');
}
public function delete($id)
{
return $this->deleteModelTrait($id,$this->setting);
}
}
- Vào AdminProductController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Components\Recusive;
use App\Http\Requests\ProductAddRequest;
use App\Product;
use App\ProductImage;
use App\ProductTag;
use App\Tag;
use App\Traits\DeleteModelTrait;
use App\Traits\StorageImageTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Storage;
class AdminProductController extends Controller
{
use StorageImageTrait,DeleteModelTrait;
private $category;
private $product;
private $productImage;
private $tag;
private $productTag;
public function __construct(Category $category,Product $product ,ProductImage $productImage,
Tag $tag,ProductTag $productTag)
{
$this->category = $category;
$this->product = $product;
$this->productImage = $productImage;
$this->tag = $tag;
$this->productTag = $productTag;
}
public function index()
{
$products = $this->product->latest()->paginate(5);
return view('admin.product.index', compact('products'));
}
public function create()
{
$htmlOption = $this->getCategory($parentId='');
return view('admin.product.add',compact('htmlOption'));
}
public function getCategory($parentId)
{
$data = $this->category->all();
$recusive = new Recusive($data);
$htmlOption = $recusive->categoryRecusive($parentId);
return $htmlOption;
}
public function store(ProductAddRequest $request)
{
try {
DB::beginTransaction();
$dataProductCreate = [
'name'=>$request->name,
'price' =>$request->price,
'content' => $request->contents,
'user_id' => auth()->id(),
'category_id'=>$request->category_id
];
$dataUploadFeatureImage = $this->storageTraitUpload($request, 'feature_image_path','product');
if (!empty($dataUploadFeatureImage)){
$dataProductCreate['feature_image_name']=$dataUploadFeatureImage['file_name'];
$dataProductCreate['feature_image_path']=$dataUploadFeatureImage['file_path'];
}
$product = $this->product->create($dataProductCreate);
//Insert data -> product_images
if ( $request->hasFile('image_path') ) {
foreach ($request->image_path as $fileItem){
$dataProductImageDetall = $this->storageTraitUploadMutiple($fileItem,'`product_images');
$product->images()->create([
'image_path'=> $dataProductImageDetall['file_path'],
'image_name'=> $dataProductImageDetall['file_name']
]);
}
}
//Insert tags -> product
if (!empty($request->tags)){
foreach ($request->tags as $tagItem){
//Insert to Tags
$tagInstance = $this->tag->firstOrCreate(['name' => $tagItem]);
$tagIds[] = $tagInstance->id;
}
}
$product->tags()->attach($tagIds);
DB::commit();
return redirect()->route('product.index');
} catch (\Exception $exception){
DB::rollBack();
log::error('Message: ' .$exception->getMessage().' --- Line : '.$exception->getLine());
}
}
public function edit($id){
$product = $this->product->find($id);
$htmlOption = $this->getCategory($product->category_id);
return view('admin.product.edit',compact('htmlOption', 'product'));
}
public function update(Request $request,$id)
{
try {
DB::beginTransaction();
$dataProductUpdate = [
'name'=>$request->name,
'price' =>$request->price,
'content' => $request->contents,
'user_id' => auth()->id(),
'category_id'=>$request->category_id
];
$dataUploadFeatureImage = $this->storageTraitUpload($request, 'feature_image_path','product');
if (!empty($dataUploadFeatureImage)){
$dataProductUpdate['feature_image_name']=$dataUploadFeatureImage['file_name'];
$dataProductUpdate['feature_image_path']=$dataUploadFeatureImage['file_path'];
}
$this->product->find($id)->update($dataProductUpdate);
$product = $this->product->find($id);
//Insert data -> product_images
if ( $request->hasFile('image_path') ) {
$this->productImage->where('product_id',$id)->delete();
foreach ($request->image_path as $fileItem){
$dataProductImageDetall = $this->storageTraitUploadMutiple($fileItem,'`product_images');
$product->images()->create([
'image_path'=> $dataProductImageDetall['file_path'],
'image_name'=> $dataProductImageDetall['file_name']
]);
}
}
//Insert tags -> product
if (!empty($request->tags)){
foreach ($request->tags as $tagItem){
//Insert to Tags
$tagInstance = $this->tag->firstOrCreate(['name' => $tagItem]);
$tagIds[] = $tagInstance->id;
}
}
$product->tags()->sync($tagIds);
DB::commit();
return redirect()->route('product.index');
} catch (\Exception $exception){
DB::rollBack();
log::error('Message: ' .$exception->getMessage().' --- Line : '.$exception->getLine());
}
}
public function delete($id)
{
return $this->deleteModelTrait($id,$this->product);
}
}
- Vào SliderAdminController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SliderAddRequest;
use App\Slider;
use App\Traits\DeleteModelTrait;
use App\Traits\StorageImageTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class SliderAdminController extends Controller
{
use StorageImageTrait,DeleteModelTrait;
private $slider;
public function __construct(Slider $slider)
{
$this->slider = $slider;
}
public function index(){
$sliders = $this->slider->latest()->paginate(5);
return view('admin.slider.index',compact('sliders'));
}
public function create(){
return view('admin.slider.add');
}
public function store(SliderAddRequest $request)
{
try {
$dataInsert = [
'name'=>$request->name,
'description'=>$request->discription
];
$dataImageSlider = $this->storageTraitUpload($request,'image_path','slider');
if (!empty($dataImageSlider)){
$dataInsert['image_name'] = $dataImageSlider['file_name'];
$dataInsert['image_path'] = $dataImageSlider['file_path'];
}
$this->slider->create($dataInsert);
return redirect()->route('slider.index');
} catch (\Exception $exception){
log::error('Message: ' .$exception->getMessage().' --- Line : '.$exception->getLine());
}
}
public function edit($id)
{
$slider = $this->slider->find($id);
return view('admin.slider.edit',compact('slider'));
}
public function update(Request $request, $id)
{
try {
$dataUpdate = [
'name'=>$request->name,
'description'=>$request->discription
];
$dataImageSlider = $this->storageTraitUpload($request,'image_path','slider');
if (!empty($dataImageSlider)){
$dataUpdate['image_name'] = $dataImageSlider['file_name'];
$dataUpdate['image_path'] = $dataImageSlider['file_path'];
}
$this->slider->find($id)->update($dataUpdate);
return redirect()->route('slider.index');
} catch (\Exception $exception){
log::error('Message: ' .$exception->getMessage().' --- Line : '.$exception->getLine());
}
}
public function delete($id)
{
return $this->deleteModelTrait($id,$this->slider);
}
}

0 Nhận xét