user(); if (!$user->isAdmin()) { // Show only users brand(s) accounts. $brands = $user->sites()->with('brand')->get()->pluck('brand') ->filter(function ($v) { return !\is_null($v); }); return $query->whereHas('brand', function ($q) use ($brands) { $q->whereIn('id', $brands->pluck('id')); }); } return $query; } /** * Crud index view * * @param \Kwaadpepper\CrudPolicies\Http\Requests\CrudIndexRequest $request * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ public function index(CrudIndexRequest $request) { /** @var \Illuminate\View\View */ $view = $this->crudIndex($request); if ($request->ajax()) { return $view; } return view('accounts.index', $view->getData()); } /** * Before store model middleware * * @param Account $model * @return void */ public function storeModel(Account &$model) { $model->brand()->associate(\request()->brand); } /** * Credit * * @param \App\Models\Brand $brand * @param \App\Models\Account $account * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed */ public function credit(Brand $brand, Account $account) { return view('accounts.credit', compact('brand', 'account')); } /** * Credit Account * * @param \App\Http\Requests\Credit\AddCreditToAccountRequest $request * @param \App\Models\Brand $brand * @param \App\Models\Account $account * @return \Illuminate\Http\RedirectResponse * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed */ public function creditSubmit(AddCreditToAccountRequest $request, Brand $brand, Account $account) { $log = new CreditLog(); $log->account()->associate($account); $log->type = BalanceOperation::credit(); $log->amount = \strval($request->amount); if ($log->saveOrFail()) { return \response()->redirectToRoute('brands.credit_accounts.index', $brand) ->with('info', trans('Le compte à bien été crédité')); } return back(); } }