123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Brand;
- use App\Models\BrandTranslation;
- use App\Models\Product;
- use Illuminate\Support\Str;
- class BrandController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- $sort_search =null;
- $brands = Brand::orderBy('name', 'asc');
- if ($request->has('search')){
- $sort_search = $request->search;
- $brands = $brands->where('name', 'like', '%'.$sort_search.'%');
- }
- $brands = $brands->paginate(15);
- return view('backend.product.brands.index', compact('brands', 'sort_search'));
- }
- /**
- * 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)
- {
- $brand = new Brand;
- $brand->name = $request->name;
- $brand->meta_title = $request->meta_title;
- $brand->meta_description = $request->meta_description;
- if ($request->slug != null) {
- $brand->slug = str_replace(' ', '-', $request->slug);
- }
- else {
- $brand->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.Str::random(5);
- }
- $brand->logo = $request->logo;
- $brand->save();
- $brand_translation = BrandTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'brand_id' => $brand->id]);
- $brand_translation->name = $request->name;
- $brand_translation->save();
- flash(translate('Brand has been inserted successfully'))->success();
- return redirect()->route('brands.index');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Request $request, $id)
- {
- $lang = $request->lang;
- $brand = Brand::findOrFail($id);
- return view('backend.product.brands.edit', compact('brand','lang'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $brand = Brand::findOrFail($id);
- if($request->lang == env("DEFAULT_LANGUAGE")){
- $brand->name = $request->name;
- }
- $brand->meta_title = $request->meta_title;
- $brand->meta_description = $request->meta_description;
- if ($request->slug != null) {
- $brand->slug = strtolower($request->slug);
- }
- else {
- $brand->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.Str::random(5);
- }
- $brand->logo = $request->logo;
- $brand->save();
- $brand_translation = BrandTranslation::firstOrNew(['lang' => $request->lang, 'brand_id' => $brand->id]);
- $brand_translation->name = $request->name;
- $brand_translation->save();
- flash(translate('Brand has been updated successfully'))->success();
- return back();
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $brand = Brand::findOrFail($id);
- Product::where('brand_id', $brand->id)->delete();
- foreach ($brand->brand_translations as $key => $brand_translation) {
- $brand_translation->delete();
- }
- Brand::destroy($id);
- flash(translate('Brand has been deleted successfully'))->success();
- return redirect()->route('brands.index');
- }
- }
|