Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/app/Http/Controllers/Shop/ |
<?php
namespace App\Http\Controllers\Shop;
use App\Models\Brand;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$brand = Brand::orderBy('id', 'DESC')->paginate();
return view('backend.brand.index')->with('brands', $brand);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('backend.brand.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'string|required',
]);
$data = $request->all();
$slug = Str::slug($request->title);
$count = Brand::where('slug', $slug)->count();
if ($count > 0) {
$slug = $slug . '-' . date('ymdis') . '-' . mt_rand(0, 999);
}
$data['slug'] = $slug;
// return $data;
$status = Brand::create($data);
if ($status) {
request()->session()->flash('success', 'Brand successfully created');
} else {
request()->session()->flash('error', 'Error, Please try again');
}
return redirect()->route('brand.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($id)
{
$brand = Brand::find($id);
if (!$brand) {
request()->session()->flash('error', 'Brand not found');
}
return view('backend.brand.edit')->with('brand', $brand);
}
/**
* 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::find($id);
$this->validate($request, [
'title' => 'string|required',
]);
$data = $request->all();
$status = $brand->fill($data)->save();
if ($status) {
request()->session()->flash('success', 'Brand successfully updated');
} else {
request()->session()->flash('error', 'Error, Please try again');
}
return redirect()->route('brand.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$brand = Brand::find($id);
if ($brand) {
$status = $brand->delete();
if ($status) {
request()->session()->flash('success', 'Brand successfully deleted');
} else {
request()->session()->flash('error', 'Error, Please try again');
}
return redirect()->route('brand.index');
} else {
request()->session()->flash('error', 'Brand not found');
return redirect()->back();
}
}
}