Advertisement
dimkiriakos

brand controller

Jul 26th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.24 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Brand;
  6. use App\Models\Multipic;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. // use Faker\Provider\Image;
  10. use Image;
  11.  
  12. class BrandController extends Controller
  13. {
  14.  
  15.     public function __construct()
  16.     {
  17.         $this->middleware('auth');
  18.     }
  19.     /**
  20.      * Display a listing of the resource.
  21.      *
  22.      * @return \Illuminate\Http\Response
  23.      */
  24.     public function index()
  25.     {
  26.         //
  27.  
  28.         /**
  29.          *  $table->string('brand_name');
  30.          *  $table->string('brand_image');
  31.          */
  32.  
  33.         $brands = Brand::OrderBy('id', 'DESC')->paginate(5);
  34.         return view('admin.brand.index', compact('brands'));
  35.     }
  36.  
  37.  
  38.     /**
  39.      * Show the form for creating a new resource.
  40.      *
  41.      * @return \Illuminate\Http\Response
  42.      */
  43.     public function create()
  44.     {
  45.         //
  46.     }
  47.  
  48.     /**
  49.      * Store a newly created resource in storage.
  50.      *
  51.      * @param  \Illuminate\Http\Request  $request
  52.      * @return \Illuminate\Http\Response
  53.      */
  54.     public function store(Request $request)
  55.     {
  56.         $MIN = 3;
  57.         $MAX = 50;
  58.         $validateData = $request->validate(
  59.             [
  60.                 'brand_name' => "required|unique:brands|min:$MIN |max:$MAX",
  61.                 'brand_image' => "required|mimes:png,jpg,jpeg",
  62.             ],
  63.             [ //here is how we are creating custom validate messages
  64.                 'brand_name.required' => 'Το πεδίο δεν μπορεί να έιναι κενό',
  65.                 'brand_name.min' => "Το όνομα του Brand πρέπει να περιέχει τουλάχιστον  $MIN  χαρακτήρες",
  66.                 'brand_name.max' => "Το όνομα του Brand πρέπει δεν μπορεί να είναι μεγαλύτερο από $MAX χαρακτήρες",
  67.             ]
  68.         );
  69.  
  70.         // Uploading Image
  71.         $brand_image = $request->file('brand_image');
  72.  
  73.         // $name_gen = hexdec(uniqid());
  74.         // $img_ext = strtolower($brand_image->getClientOriginalExtension());
  75.         // $img_name = $name_gen . '.' . $img_ext;
  76.         // $up_location = 'image/brand/';
  77.         // $last_img = $up_location . $img_ext;
  78.         // $brand_image->move($up_location, $img_name);
  79.  
  80.         $img_ext = strtolower($brand_image->getClientOriginalExtension());
  81.         $name_gen = hexdec(uniqid()) . '.' . $img_ext;
  82.         $up_location = 'image/brand/';
  83.         Image::make($brand_image)->resize(300, 200)->save($up_location . $name_gen);
  84.  
  85.  
  86.         $brand = new Brand();
  87.  
  88.         $brand->brand_name = $request->brand_name;
  89.         $brand->brand_image = $name_gen;
  90.         $brandName = $brand->name;
  91.         // dd($brand_image, $brand);
  92.         $brand->save();
  93.  
  94.         $notification = array(
  95.             'message' => 'Brand Created Successfully',
  96.             'alert-type' => 'warning'
  97.         );
  98.  
  99.         // return redirect()->back()->with('success', "Το brand {$brandName} αποθηκέυτηκε επιτυχώς");
  100.         return redirect()->back()->with($notification);
  101.     }
  102.  
  103.     /**
  104.      * Display the specified resource.
  105.      *
  106.      * @param  \App\Models\Brand  $brand
  107.      * @return \Illuminate\Http\Response
  108.      */
  109.     public function show(Brand $brand)
  110.     {
  111.         //
  112.     }
  113.  
  114.     /**
  115.      * Show the form for editing the specified resource.
  116.      *
  117.      * @param  \App\Models\Brand  $brand
  118.      * @return \Illuminate\Http\Response
  119.      */
  120.     public function edit($id)
  121.     {
  122.         $brand = Brand::find($id);
  123.         return view('admin.brand.edit', compact('brand'));
  124.     }
  125.  
  126.     /**
  127.      * Update the specified resource in storage.
  128.      *
  129.      * @param  \Illuminate\Http\Request  $request
  130.      * @param  \App\Models\Brand  $brand
  131.      * @return \Illuminate\Http\Response
  132.      */
  133.     public function update(Request $request, $id)
  134.     {
  135.  
  136.         //
  137.         $MIN = 3;
  138.         $MAX = 50;
  139.         $validateData = $request->validate(
  140.             [
  141.                 'brand_name' => "required|min:$MIN |max:$MAX",
  142.                 'brand_image' => "mimes:png,jpg,jpeg",
  143.             ],
  144.             [ //here is how we are creating custom validate messages
  145.                 'brand_name.required' => 'Το πεδίο δεν μπορεί να έιναι κενό',
  146.                 'brand_name.min' => "Το όνομα του Brand πρέπει να περιέχει τουλάχιστον  $MIN  χαρακτήρες",
  147.                 'brand_name.max' => "Το όνομα του Brand πρέπει δεν μπορεί να είναι μεγαλύτερο από $MAX χαρακτήρες",
  148.             ]
  149.         );
  150.  
  151.         $brand = Brand::find($id);
  152.  
  153.  
  154.         // Uploading Image
  155.         $old_image = $request->old_image;
  156.         if ($request->file('brand_image')) {
  157.             // Uploading Image
  158.             $brand_image = $request->file('brand_image');
  159.  
  160.             // $name_gen = hexdec(uniqid());
  161.             // $img_ext = strtolower($brand_image->getClientOriginalExtension());
  162.             // $img_name = $name_gen . '.' . $img_ext;
  163.             // $up_location = 'image/brand/';
  164.             // $last_img = $up_location . $img_ext;
  165.             // $brand_image->move($up_location, $img_name);
  166.  
  167.             $img_ext = strtolower($brand_image->getClientOriginalExtension());
  168.             $name_gen = $old_image;
  169.             $up_location = 'image/brand/';
  170.             Image::make($brand_image)->resize(300, 200)->save($up_location . $name_gen);
  171.         }
  172.  
  173.  
  174.  
  175.         $brand->brand_name = $request->brand_name;
  176.         if (!$request->file('brand_image')) {
  177.  
  178.             $brand->brand_image = $old_image;
  179.         }
  180.         $brandName = $brand->brand_name;
  181.         $brand->save();
  182.         return redirect()->route('all.brands')->with('success', "Το brand {$brandName} ανανεώθηκε επιτυχώς");
  183.     }
  184.  
  185.     /**
  186.      * Remove the specified resource from storage.
  187.      *
  188.      * @param  \App\Models\Brand  $brand
  189.      * @return \Illuminate\Http\Response
  190.      */
  191.     public function destroy($id)
  192.     {
  193.         $brand =  Brand::find($id);
  194.         $location = 'image/brand/';
  195.         $brandName = $brand->brand_name;
  196.         $brand_image = $brand->brand_image;
  197.         $brandImage = $location . $brand_image;
  198.         $brand->delete();
  199.         unlink($brandImage);
  200.         return redirect()->back()->with('success', "To Brand {$brandName} Διαγράφηκε επιτχώς");
  201.     }
  202.  
  203.  
  204.     // Multi image
  205.  
  206.     public function Multipicture()
  207.     {
  208.         $images = Multipic::all();
  209.         return view('admin.multipic.index', compact('images'));
  210.     }
  211.  
  212.     public function storeImage(Request $request)
  213.     {
  214.         $image = $request->file('image');
  215.  
  216.         foreach ($image as $multi_image) {
  217.  
  218.             $img_ext = strtolower($multi_image->getClientOriginalExtension());
  219.             $name_gen = hexdec(uniqid()) . '.' . $img_ext;
  220.             $up_location = 'image/multi/';
  221.             $full_up_location = 'image/multi/full/';
  222.             Image::make($multi_image)->resize(300, 200)->save($up_location . $name_gen);
  223.             Image::make($multi_image)->save($full_up_location . $name_gen);
  224.  
  225.  
  226.             $multiImg = new Multipic();
  227.  
  228.             $multiImg->image = $name_gen;
  229.  
  230.             // dd($brand_image, $brand);
  231.             $multiImg->save();
  232.         }
  233.  
  234.         return redirect()->back()->with('success', "Οι εικόνες αποθηκέυτηκαν επιτυχώς");
  235.     }
  236.  
  237.     public function imageDelete($id)
  238.     {
  239.         $image = Multipic::find($id);
  240.  
  241.         // dd($image->id);
  242.         $location = 'image/multi/';
  243.         $full_location = 'image/multi/full/';
  244.         $img_and_loc = $location . $image->image;
  245.         $full_img_and_loc = $full_location . $image->image;
  246.         unlink($img_and_loc);
  247.         unlink($full_img_and_loc);
  248.         $image->delete();
  249.         return redirect()->back()->with('success', 'Η εικόνα διαγράφηκε επιτυχώς');
  250.     }
  251.  
  252.     public function showImage($id)
  253.     {
  254.         $image = Multipic::find($id);
  255.         return view('admin.multipic.show', compact('image'));
  256.     }
  257.  
  258.     public function Logout()
  259.     {
  260.         Auth::logout();
  261.         return redirect()->route('login')->with('success', 'User Logged Out');
  262.     }
  263. }
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement