CategorieController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Http\Controllers\backend;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\Categorie;
  6. use App\Models\Produit;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Facades\Redirect;
  9. use App\Models\temporaryFile;
  10. class CategorieController extends Controller
  11. {
  12. public function Index(){
  13. $articles = Categorie::get();
  14. return view("backend.category.index",compact('articles'));
  15. }
  16. public function create(){
  17. return view('backend.category.add');
  18. }
  19. public function store( Request $request){
  20. $tmp_file = temporaryFile::where('folder',$request->image)->first();
  21. if($tmp_file){
  22. Storage::copy('image/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'image/'.$tmp_file->folder.'/'.$tmp_file->file);
  23. Categorie::create(
  24. [
  25. 'nom' => $request->nom,
  26. 'image' => $tmp_file->folder . '/' . $tmp_file->file,
  27. ],
  28. );
  29. Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
  30. $tmp_file->delete();
  31. return redirect('categories');
  32. }
  33. }
  34. public function show($id){
  35. $article=Categorie::find($id);
  36. return view('backend.category.show',['article'=>$article]);
  37. }
  38. public function edit($id) {
  39. $article = Categorie::find($id);
  40. return view('backend.category.edit',['article'=>$article]);
  41. }
  42. public function update(Request $request, $id) {
  43. $article = Categorie::find($id);
  44. $article->nom = $request->input('nom');
  45. $tmp_file = temporaryFile::where('folder',$request->image)->first();
  46. if($tmp_file){
  47. Storage::copy('image/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'image/'.$tmp_file->folder.'/'.$tmp_file->file);
  48. $article->update([
  49. "image" => $tmp_file->folder . '/' . $tmp_file->file
  50. ]);
  51. Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
  52. $tmp_file->delete();
  53. }
  54. $article->created_at = \Carbon\Carbon::now();
  55. $article->save();
  56. return redirect ('categories');
  57. }
  58. public function destroy(Request $request, $id){
  59. // $article = Categorie::find($id);
  60. // $article->delete();
  61. // Produit::whereCategoryId($id)->update(['category_id' => null]);
  62. $products = Produit::where('category_id', $id)->count();
  63. if($products > 0){
  64. return redirect ('categories')
  65. ->with('message', 'Produit existe dans cette categorie');
  66. }
  67. else{
  68. $category= Categorie::find($id);
  69. $category->delete();
  70. return Redirect::to('categories')
  71. ->with('message', 'Categorie supprimée');
  72. }
  73. return back();
  74. }
  75. public function changeStatus(Request $request)
  76. {
  77. $article = Categorie::find($request->Id);
  78. $article->etat = $request->etat;
  79. $article->save();
  80. return response()->json(['success'=>'Status change successfully.']);
  81. }
  82. public function fileUpload(Request $request){
  83. if($request->hasfile('image')){
  84. $image = $request->file('image');
  85. $file = $image->getClientOriginalName();
  86. $folder = uniqid('post', 'true');
  87. $image->storeAs('image/tmp/' . $folder,$file);
  88. TemporaryFile::create([
  89. 'folder' => $folder,
  90. 'file'=> $file
  91. ]);
  92. return $folder;
  93. }
  94. return '';
  95. }
  96. public function deleteUpload(){
  97. $tmp_file = TemporaryFile::where('folder',request()->getContent())->first();
  98. if($tmp_file){
  99. Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
  100. $tmp_file->delete();
  101. return response('');
  102. }
  103. }
  104. }