user(); if (!$user->isAdmin()) { $brandModels = $user->brands()->pluck('id'); } else { $brandModels = $request->brand ?? Brand::all()->pluck('id'); } /** @var \Illuminate\Database\Eloquent\Builder $credit_accountModels */ $credit_accountModels = Account::query()->whereIn('brand_id', $brandModels); /** @var string $search Search field */ $search = $request->search; if ($search) { $this->searchQuery( $credit_accountModels, $search, null, 'label', 'balance', ); } $searchFields = trans('Label, solde'); /** Sort columns with a query */ $this->sortQuery($credit_accountModels); /** Custom pagination */ $credit_accountModels = $this->customPaginate($credit_accountModels, $request->pagination); return view('bo.pages.credit_accounts.index', compact( 'credit_accountModels', 'brandModels', 'search', 'searchFields' )); } /** * Show the form for creating a new resource. * * @param \App\Models\Brand $brand * @param \App\Models\Account $account * @return \Illuminate\Contracts\View\View */ public function create(Brand $brand, Account $account): \Illuminate\Contracts\View\View { return view('bo.pages.credit_accounts.create', [ 'brandModel' => $brand, 'credit_accountModel' => $account ]); } /** * Store a newly created resource in storage. * * @param \App\Models\Brand $brand * @param \App\Http\Requests\Bo\CreditAccounts\StoreCreditAccountRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(Brand $brand, StoreCreditAccountRequest $request): \Illuminate\Http\RedirectResponse { return DB::transaction(function () use ($brand, $request) { $credit_accountModel = new Account(); $credit_accountModel->brand_id = $brand->id; $credit_accountModel->fill($request->validated()); if ($credit_accountModel->saveOrFail()) { return redirect() ->route('bo.brands.credit_accounts.index', $brand) ->with('success', trans( 'crud.messages.has_been_created', ['model' => Str::singular(trans('models.classes.accounts'))] )); } return redirect() ->back() ->with('error', trans( 'crud.messages.cannot_be_created', ['model' => Str::singular(trans('models.classes.accounts'))] )); }); } }