Undefined variable $request I am trying to insert and update 2 tables at the same time but i got the following Error Undefined variable $request
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Account;
use App\Models\User;
use App\Models\Transfer;
use DB;
use Auth;
class TransferController extends Controller
{
public function transferform(Request $request)
{
$user = Auth::user();
$accounts = User::find($user->id)->accounts;
$senderAccountId = $accounts->first()->id;
$amount = $request->input('amt');
DB::transaction(function () use ($amount, $senderAccountId) {
$transfer = new Transfer();
$transfer = $amount;
$transfer = $senderAccountId;
$transfer->actype = $request->input('actype');
$transfer->acnumber = $request->input('acnumber');
$transfer->to_acc_name = $request->input('to_acc_name');
$transfer->amt = $amount;
$transfer->Frequency = $request->input('Frequency');
$transfer->acbalance = $request->input('acbalance');
$transfer->discription = $request->input('discription');
$transfer->status = 'pending';
$transfer->save();
$senderAccount = Account::where('id', '=', $senderAccountId)->first();
$senderAccount->acnumber =- $amount;
$senderAccount->save();
Account::query()->findOrFail($senderAccountId)->increment('balance', $amount);
Account::query()->findOrFail($recipientAccountId)->decrement('balance', $amount);
$transfer->status = 'complete';
$transfer->save();
});
}}
Here is my route:
Route::post('transfer-form', [TransferController::class, 'transferform']);
After passing is this way
DB::transaction(function () use ($amount, $senderAccountId, $request) {
I got the following error:
Attempt to assign property "actype" on int
CodePudding user response:
Pay attention at this part. You have reassign it with something else. The $transfer
value have been override.
$transfer = new Transfer();
$transfer = $amount;
$transfer = $senderAccountId;
// try change to
$transfer = new Transfer();
$transfer->amt = $amount;
$transfer->senderAccountId = $senderAccountId;