client = $client; } /** * Display a listing of the resource. */ public function index() { if (request()->ajax()) { $model = Client::query()->orderBy('order'); return DataTables::eloquent($model) ->addIndexColumn() ->setRowClass('tableRow') ->editColumn('logo', function (Client $client) { return $client->getRawOriginal('logo') ? "{$client->name}" : '-'; }) ->editColumn('title', function (Client $client) { $route = route('client.show', $client->id); $html ="{{ $client->name }}"; return $html; }) ->editColumn('manager_name', function (Client $client) { return $client->manager_name ?? "-"; }) ->editColumn('status', function (Client $client) { $status = $client->status ? 'Published' : 'Draft'; $color = $client->status ? 'text-success' : 'text-danger'; return "

{$status}

"; }) ->addColumn('action', 'client::client.datatable.action') ->rawColumns(['status', 'logo', 'action']) ->toJson(); } return view('client::client.index', [ 'title' => 'Client List', ]); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = 'Create Client'; $data['editable'] = false; return view('client::client.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $client = null; $validated = $request->validate([ "name" => ["required", "string", "max:255"], ]); $request->merge([ "company_name" => $request->name, 'order' => ($maxOrder = Client::max('order')) !== null ? ++$maxOrder : 1, ]); try { DB::transaction(function () use ($request, &$client) { $input = $request->only(Client::getFillableFields()); $client = Client::create($input); }); flash()->success("Client {$client->name} has been created"); return redirect()->route('client.index'); } catch (\Exception $e) { flash()->error($e->getMessage()); return redirect()->back()->withInput(); } } /** * Show the specified resource. */ public function show($id) { $data["title"] = "Show Client"; $data["client"] = $this->client->findById($id); return view('client::client.show', $data); } /** * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = 'Edit Client'; $data['editable'] = true; $data["client"] = $this->client->findById($id); return view('client::client.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id) { $validated = $request->validate([ "title" => ["required"], ]); try { DB::transaction(function () use ($request, $id) { $input = $request->only(Client::getFillableFields()); $client = $this->client->update($id, $input); }); flash()->success('Client has been updated!'); return redirect()->route('client.index'); } catch (\Throwable $th) { flash()->error($th->getMessage()); return redirect()->back()->withInput(); } } /** * Remove the specified resource from storage. */ public function destroy($id) { try { DB::transaction(function () use ($id) { $client = $this->client->delete($id); }); return response()->json([ 'status' => 200, 'message' => 'Client has been deleted!', ], 200); } catch (\Throwable $th) { return response()->json([ 'status' => 500, 'message' => 'Failed to delete client!', 'error' => $th->getMessage(), ], 500); } } }