resultoffice's avatar

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']);

0 likes
12 replies
Tjyoung's avatar

have your try, following way?

request()->actype; 
siangboon's avatar

i think you may need to pass the $request into function ()

xoca's avatar

Pass the request into the function with use, like you are doing for $amount and $senderAccountId.

DB::transaction(function () use ($amount, $senderAccountId, $request) {
resultoffice's avatar

@xoca after passing is this way

DB::transaction(function () use ($amount, $senderAccountId, $request) {

I got the following error: Attempt to assign property "actype" on int

JussiMannisto's avatar

@resultoffice You create a Transfer object as $transfer but then overwrite it with the $amount and $senderAccountId variables. The error message tells you what's wrong: $transfer is an integer and trying to set $transfer->actype causes the error.

I assume you meant to set $transfer->amount and $transfer->senderAccountId instead.

antaugustol's avatar

@resultoffice I think you made a mistake here:

$transfer = $amount;
$transfer = $senderAccountId;

should be something like:

$transfer->amount = $amount;
$transfer->senderAccountId = $senderAccountId;

as @jussimannisto had pointed out.

resultoffice's avatar
resultoffice
OP
Best Answer
Level 1

@antaugustol Thanks I have solve it with this same approach, i wasn't paying enough attention to the code, thanks for your insight .

1 like

Please or to participate in this conversation.