Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Models\Brand;
- use App\Models\Multipic;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- // use Faker\Provider\Image;
- use Image;
- class BrandController extends Controller
- {
- public function __construct()
- {
- $this->middleware('auth');
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- /**
- * $table->string('brand_name');
- * $table->string('brand_image');
- */
- $brands = Brand::OrderBy('id', 'DESC')->paginate(5);
- return view('admin.brand.index', compact('brands'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $MIN = 3;
- $MAX = 50;
- $validateData = $request->validate(
- [
- 'brand_name' => "required|unique:brands|min:$MIN |max:$MAX",
- 'brand_image' => "required|mimes:png,jpg,jpeg",
- ],
- [ //here is how we are creating custom validate messages
- 'brand_name.required' => 'Το πεδίο δεν μπορεί να έιναι κενό',
- 'brand_name.min' => "Το όνομα του Brand πρέπει να περιέχει τουλάχιστον $MIN χαρακτήρες",
- 'brand_name.max' => "Το όνομα του Brand πρέπει δεν μπορεί να είναι μεγαλύτερο από $MAX χαρακτήρες",
- ]
- );
- // Uploading Image
- $brand_image = $request->file('brand_image');
- // $name_gen = hexdec(uniqid());
- // $img_ext = strtolower($brand_image->getClientOriginalExtension());
- // $img_name = $name_gen . '.' . $img_ext;
- // $up_location = 'image/brand/';
- // $last_img = $up_location . $img_ext;
- // $brand_image->move($up_location, $img_name);
- $img_ext = strtolower($brand_image->getClientOriginalExtension());
- $name_gen = hexdec(uniqid()) . '.' . $img_ext;
- $up_location = 'image/brand/';
- Image::make($brand_image)->resize(300, 200)->save($up_location . $name_gen);
- $brand = new Brand();
- $brand->brand_name = $request->brand_name;
- $brand->brand_image = $name_gen;
- $brandName = $brand->name;
- // dd($brand_image, $brand);
- $brand->save();
- $notification = array(
- 'message' => 'Brand Created Successfully',
- 'alert-type' => 'warning'
- );
- // return redirect()->back()->with('success', "Το brand {$brandName} αποθηκέυτηκε επιτυχώς");
- return redirect()->back()->with($notification);
- }
- /**
- * Display the specified resource.
- *
- * @param \App\Models\Brand $brand
- * @return \Illuminate\Http\Response
- */
- public function show(Brand $brand)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Models\Brand $brand
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $brand = Brand::find($id);
- return view('admin.brand.edit', compact('brand'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param \App\Models\Brand $brand
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- //
- $MIN = 3;
- $MAX = 50;
- $validateData = $request->validate(
- [
- 'brand_name' => "required|min:$MIN |max:$MAX",
- 'brand_image' => "mimes:png,jpg,jpeg",
- ],
- [ //here is how we are creating custom validate messages
- 'brand_name.required' => 'Το πεδίο δεν μπορεί να έιναι κενό',
- 'brand_name.min' => "Το όνομα του Brand πρέπει να περιέχει τουλάχιστον $MIN χαρακτήρες",
- 'brand_name.max' => "Το όνομα του Brand πρέπει δεν μπορεί να είναι μεγαλύτερο από $MAX χαρακτήρες",
- ]
- );
- $brand = Brand::find($id);
- // Uploading Image
- $old_image = $request->old_image;
- if ($request->file('brand_image')) {
- // Uploading Image
- $brand_image = $request->file('brand_image');
- // $name_gen = hexdec(uniqid());
- // $img_ext = strtolower($brand_image->getClientOriginalExtension());
- // $img_name = $name_gen . '.' . $img_ext;
- // $up_location = 'image/brand/';
- // $last_img = $up_location . $img_ext;
- // $brand_image->move($up_location, $img_name);
- $img_ext = strtolower($brand_image->getClientOriginalExtension());
- $name_gen = $old_image;
- $up_location = 'image/brand/';
- Image::make($brand_image)->resize(300, 200)->save($up_location . $name_gen);
- }
- $brand->brand_name = $request->brand_name;
- if (!$request->file('brand_image')) {
- $brand->brand_image = $old_image;
- }
- $brandName = $brand->brand_name;
- $brand->save();
- return redirect()->route('all.brands')->with('success', "Το brand {$brandName} ανανεώθηκε επιτυχώς");
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Models\Brand $brand
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $brand = Brand::find($id);
- $location = 'image/brand/';
- $brandName = $brand->brand_name;
- $brand_image = $brand->brand_image;
- $brandImage = $location . $brand_image;
- $brand->delete();
- unlink($brandImage);
- return redirect()->back()->with('success', "To Brand {$brandName} Διαγράφηκε επιτχώς");
- }
- // Multi image
- public function Multipicture()
- {
- $images = Multipic::all();
- return view('admin.multipic.index', compact('images'));
- }
- public function storeImage(Request $request)
- {
- $image = $request->file('image');
- foreach ($image as $multi_image) {
- $img_ext = strtolower($multi_image->getClientOriginalExtension());
- $name_gen = hexdec(uniqid()) . '.' . $img_ext;
- $up_location = 'image/multi/';
- $full_up_location = 'image/multi/full/';
- Image::make($multi_image)->resize(300, 200)->save($up_location . $name_gen);
- Image::make($multi_image)->save($full_up_location . $name_gen);
- $multiImg = new Multipic();
- $multiImg->image = $name_gen;
- // dd($brand_image, $brand);
- $multiImg->save();
- }
- return redirect()->back()->with('success', "Οι εικόνες αποθηκέυτηκαν επιτυχώς");
- }
- public function imageDelete($id)
- {
- $image = Multipic::find($id);
- // dd($image->id);
- $location = 'image/multi/';
- $full_location = 'image/multi/full/';
- $img_and_loc = $location . $image->image;
- $full_img_and_loc = $full_location . $image->image;
- unlink($img_and_loc);
- unlink($full_img_and_loc);
- $image->delete();
- return redirect()->back()->with('success', 'Η εικόνα διαγράφηκε επιτυχώς');
- }
- public function showImage($id)
- {
- $image = Multipic::find($id);
- return view('admin.multipic.show', compact('image'));
- }
- public function Logout()
- {
- Auth::logout();
- return redirect()->route('login')->with('success', 'User Logged Out');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement