CategorieController.php 4.2 KB

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