Tashi Tobgay Tashi Tobgay - 1 year ago 62
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();

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 Source

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

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download