Tashi Tobgay Tashi Tobgay - 5 months ago 18
PHP Question

Store max substring value

$exmp = DB::table('cr_tblcomplaintregistration')
->select(DB::raw('MAX(CAST(SUBSTRING(complaintRegNo,16,length(complaintRegNo))+1 AS int))'))->first();
dd($exmp);


I get:

{#337 ▼
+"MAX(CAST(SUBSTRING(complaintRegNo,16,length(complaintRegNo))+1 AS int))": 51
}


How do i store the just the value 51 in another variable?.

Answer

I'm assuming the issue is the varable name? If so, you can add an SQL alias:

->select(
DB::raw('MAX(CAST(SUBSTRING(complaintRegNo,16,length(complaintRegNo))+1 AS int)) AS maximum')
)

If you want $exmp to be the column value, just add that property to the end of your chain:

$exmp = DB::table('cr_tblcomplaintregistration')
->select(DB::raw('MAX(CAST(SUBSTRING(complaintRegNo,16,length(complaintRegNo))+1 AS int)) as maximum'))
->first()
->maximum;

Of course, you may want to get if first() is not null before hand.