145 lines
4.3 KiB
PHP
145 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Brands\StoreBrandRequest;
|
|
use App\Http\Requests\Bo\Brands\UpdateBrandRequest;
|
|
use App\Models\Brand;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class BrandController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $brand */
|
|
$brand = Brand::query();
|
|
|
|
if ($request->search) {
|
|
$this->searchQuery(
|
|
$brand,
|
|
$request->search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
|
|
$this->sortQuery($brand);
|
|
$searchFields = trans('Name');
|
|
|
|
$brandModels = $brand->paginate(10);
|
|
|
|
return view('bo.pages.brands.index', compact('brandModels', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bo.pages.brands.create');
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\StoreBrandRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(StoreBrandRequest $request)
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$brand = new Brand();
|
|
$brand->fill($request->validated());
|
|
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.edit', $brand)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function edit(Brand $brand)
|
|
{
|
|
return view('bo.pages.brands.edit', ['brandModel' => $brand]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Brands\UpdateBrandRequest $request
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(UpdateBrandRequest $request, Brand $brand)
|
|
{
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$brand->fill($request->validated());
|
|
if ($brand->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.edit', $brand)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
} //end if
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Brand $brand)
|
|
{
|
|
if ($brand->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.index')
|
|
->with('success', trans(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(trans('models.classes.brands'))]
|
|
));
|
|
}
|
|
}
|