Answer the question
In order to leave comments, you need to log in
How to understand Laravel controllers?
Greetings! Completely confused about Laravel controllers.
The essence of the problem is that I create a form for replenishing / withdrawing money from the balance, creating a BalanceController for this and creating methods increase (for replenishment) and decrease (for withdrawal) in it:
Here is the form:
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['[email protected]', $user->balance]]) !!}
{!! Form::token() !!}
{!! Form::label('amount', 'Введите сумму для пополнения') !!}
{!! Form::text('amount', null, ['class' => 'form-control']) !!}
{!! Form::submit('Пополнить', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['[email protected]', $user->balance]]) !!}
{!! Form::token() !!}
{!! Form::label('amount', 'Введите сумму для снятия') !!}
{!! Form::text('amount', null, ['class' => 'form-control']) !!}
{!! Form::submit('Снять', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
class BalanceController extends Controller
{
public function increase(Request $request, $id)
{
$user = User::findOrFail($id);
$amount = $user->balance->amount;
$balance = $amount + $request->amount;
$user->balance->update(['amount' => $balance]);
return redirect('profile');
}
public function decrease(Request $request, $id)
{
$user = User::findOrFail($id);
$amount = $user->balance->amount;
$balance = $amount - $request->amount;
$user->balance->update(['amount' => $balance]);
return redirect('profile');
}
}
Action App\Http\Controllers\[email protected] not defined
Route::resource('/profile', 'ProfileController');
Answer the question
In order to leave comments, you need to log in
Could the problem be caused by the fact that I did not specify any links to my BalanceController in the route?Of course, this is exactly what caused the problem.
If so, what should be specified in the route? Route::get, Route::put or something else?You have specified the PATCH method in the form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question