HOME


Mini Shell 1.0
La Pieza.DO | Todo lo que buscas!

Bienvenido de nuevo!

Acceso Cuenta Delivery
DIR: /var/www/devs.lapieza.net/app/Http/Controllers/
Upload File :
Current File : /var/www/devs.lapieza.net/app/Http/Controllers/BrandController.php
<?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

{

    public function __construct() {

        // Staff Permission Check

        $this->middleware(['permission:view_all_brands'])->only('index');

        $this->middleware(['permission:add_brand'])->only('create');

        $this->middleware(['permission:edit_brand'])->only('edit');

        $this->middleware(['permission:delete_brand'])->only('destroy');

    }

    /**

     * 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()

    {

        // EN esta para estaria el request del parent id.

    }



    /**

     * 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');



    }

}