CategorieController.php 4.1 KB

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