Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

qyioma's avatar

Object of class stdClass could not be converted to string

I want to make a label ID that need to contain code like, code-yymd-3 running no. I try to get the code from one of the table, however this error come out. I need to get the latest data in column code. Can you teach me how to fix this? I try to use json_encode, but the ouput become {"code":"CB"}20221209-001 , what I want is, CB20221209-001 .

//last data input

  $code = DB::table('ir_getweights')->select('code')->orderBy('id','desc')->first();
    $date = Carbon::now()->format('Ymd');
   // $code = json_encode($getcode);
    $number = intval($numberString);
    $newNumber = $number + 1;
    $newStringNumber = $code.$date."-".str_pad($newNumber, 3, '0', STR_PAD_LEFT);
0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

You will have to use the column it won't just contain that value as ->first() returns an object not a value, like this:

    $newStringNumber = $code->code.$date."-".str_pad($newNumber, 3, '0', STR_PAD_LEFT);
1 like

Please or to participate in this conversation.