0

I've got error on this line $number = "$autonumber[0]/$automonth/$autoyear/proforma";. This is error: https://flareapp.io/share/qm1WG17d I am trying to make function witch will be adding number of proform in month to other data. I don't know what i am doing wrong. Anybody help me?

This is controller with this function witch generates error:

public function store(Request $request)
{
    $autoyear = date('Y');
    $automonth = date('m');

    $autonumber = DB::table('proforms')
        ->select(DB::raw('MAX(autonumber) as autonumber'))
        ->where('automonth', '=', '$automonth')
        ->where('autoyear', '=', '$autoyear')
        ->get();
    $autonumber[0]++;
    $number = "$autonumber[0]/$automonth/$autoyear/proforma";
    DB::table('proforms')->insert(
        ['autonumber' => $number, 'automonth' => $automonth, 'autoyear' => $autoyear]
    );

    request()->validate([
        'user_id' => 'required',
        'proformdate' => 'required',
        'selldate' => 'required',
        'paymentmethod' => 'required',
        'paymentdate' => 'required',
        'status' => 'nullable',
        'city' => 'nullable',
        'comments' => 'nullable',
        'name' => 'required',
        'PKWIU' => 'nullable',
        'quantity' => 'required',
        'unit' => 'required',
        'netunit' => 'required',
        'nettotal' => 'required',
        'VATrate' => 'required',
        'grossunit' => 'required',
        'grosstotal' => 'required',

    ]);

    Proform::create($request->all());

    return redirect()->route('proforms.index')
        ->with('success', 'Proform created successfully.');
}

After dd($autonumber); I get:

Illuminate\Support\Collection {#1328 ▼
  #items: array:1 [▼
    0 => {#1324 ▼
      +"autonumber": null
    }
  ]
}

After print_r($autonumber);

  Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [autonumber] => ) ) )
6
  • print_r($autonumber) to see what is it. Commented Aug 10, 2020 at 9:09
  • Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [autonumber] => ) ) ) Commented Aug 10, 2020 at 9:15
  • 1
    either you use ->value('autonumber') or still use ->get() and reach the inner dimension $autonumber[0]->autonumber Commented Aug 10, 2020 at 9:19
  • Please share the error message in your question, along with your debugging attempts Commented Aug 10, 2020 at 11:23
  • Object of class stdClass could not be converted to string Commented Aug 10, 2020 at 11:31

1 Answer 1

1

There is a misunderstanding, the following code will not return an integer, but an object which has the following structure: {"autonumber": 42}.

$autonumber = DB::table('proforms')
    ->select(DB::raw('MAX(autonumber) as autonumber'))
    ->where('automonth', '=', '$automonth')
    ->where('autoyear', '=', '$autoyear')
    ->get();

Change the code to work with that structure.

$autonumber[0]->autonumber++;
$number = $autonumber[0]->autonumber;
$number = "$number/$automonth/$autoyear/proforma";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.