File manager - Edit - /var/www/payraty/inventory_main/app/Http/Controllers/Admin/Attribute/AttributesController.php
Back
<?php namespace App\Http\Controllers\Admin\Attribute; use App\Http\Controllers\Controller; use App\DataTables\AttributeDataTable; use App\Http\Requests\AttributeRequest; use App\Services\Attribute\AttributeService; use Illuminate\Http\Request; use App\Models\Attribute; use Illuminate\Support\Facades\Auth; use PDF; use Excel; use App\Exports\AttributesExport; use Illuminate\Support\Facades\DB; class AttributesController extends Controller { protected $attributeService; /** * __construct * * @param mixed $attributeService * @return void */ public function __construct(AttributeService $attributeService) { $this->attributeService = $attributeService; $this->middleware(['permission:List Attribute'])->only(['index']); $this->middleware(['permission:Add Attribute'])->only(['create']); $this->middleware(['permission:Edit Attribute'])->only(['edit']); $this->middleware(['permission:Delete Attribute'])->only(['destroy']); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(AttributeDataTable $dataTable) { set_page_meta(__('custom.attribute')); $user = Auth::user(); $orgId = $user->organisation_id; $attributes = Attribute::where('organisation_id', $orgId)->paginate(10); // return $dataTable->render('admin.attributes.index'); $profile = DB::table('profiles')->where('user_id', $user->id)->first(); return view('admin.attributes.index', compact('attributes', 'profile')); } public function filter(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $period = $request->get('period', 'all'); $query = Attribute::where('organisation_id', $orgId)->with('items'); switch ($period) { case 'monthly': $query->whereMonth('created_at', now()->month); break; case 'yearly': $query->whereYear('created_at', now()->year); break; // 'all' case doesn't need additional filtering } $attributes = $query->paginate(10); if ($request->ajax()) { return response()->json([ 'attributes' => $attributes ]); } // Fallback for non-AJAX requests return redirect()->route('admin.attributes.index'); } public function search(Request $request) { $user = Auth::user(); $orgId = $user->organisation_id; $searchTerm = $request->get('q'); $attributes = Attribute::where('organisation_id', $orgId) ->with('items') ->where(function ($query) use ($searchTerm) { $query->where('name', 'LIKE', "%{$searchTerm}%"); }) ->paginate(10); if ($request->ajax()) { return response()->json([ 'attributes' => $attributes ]); } // Fallback for non-AJAX requests return view('admin.attributes.index', compact('attributes')); } /** * exportAttributes * * @param mixed $request * @return void */ public function exportAttributes(Request $request) { $attributes = []; $type = $request->type; $user = Auth::user(); $orgId = $user->organisation_id; $attributes = Attribute::where('organisation_id', $orgId)->get(); $name = 'Attribute_' . now()->format('YmdHis'); if ($type == 'pdf') { $pdf = PDF::loadView('admin.attributes.pdf.index', ['attributes' => $attributes]); return $pdf->download($name . '.pdf'); } else if ($type == 'csv') { return Excel::download(new AttributesExport($attributes), $name . '.csv'); } else if ($type == 'excel') { return Excel::download(new AttributesExport($attributes), $name . '.xlsx'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { set_page_meta(__('custom.add_attribute')); return view('admin.attributes.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(AttributeRequest $request) { $data = $request->validated(); $user = Auth::user(); $orgId = $user->organisation_id; if ($this->attributeService->createOrUpdate($orgId, $data, null)) { flash(__('custom.attribute_create_successful'))->success(); } else { flash(__('custom.attribute_create_failed'))->error(); } return redirect()->route('admin.attributes.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) { $attribute = $this->attributeService->get($id, ['items']); set_page_meta(__('custom.edit_attribute')); return view('admin.attributes.edit', compact('attribute')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(AttributeRequest $request, $id) { $data = $request->validated(); $user = Auth::user(); $orgId = $user->organisation_id; if ($this->attributeService->createOrUpdate($orgId, $data, $id)) { flash(__('custom.attribute_updated_successful'))->success(); } else { flash(__('custom.attribute_updated_failed'))->error(); } return redirect()->route('admin.attributes.index'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if ($this->attributeService->delete($id)) { flash(__('custom.attribute_deleted_successful'))->success(); } else { flash(__('custom.attribute_deleted_failed'))->error(); } return redirect()->route('admin.attributes.index'); } // HANDLE AJAX /** * attributeItems * * @param mixed $id * @return void */ public function attributeItems($id) { return $this->attributeService->getItemsByAttributeId($id); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings