Home > Software design >  Laravel Eloquent select first Characters before the decimal point
Laravel Eloquent select first Characters before the decimal point

Time:11-06

I have a database table that has a column with data which is a mix of both characters and numbers. What I would like to achieve is select all characters and numbers before the decimal point. Below is how the data is in the column:

PO995.1
PO995.2
PO995.3
PO995.4

Eloquent Query

 $po_number= DB::table('supplierorders')
                    ->select('supplierorders.*')
                    ->orderby('po_number','asc')
                    ->get();

I would like to only select PO995 and ignore .xx Is there a way I can achieve this within my query?

Thanks.

CodePudding user response:

Yes, what you can try are using Laravel Collection Map and PHP string explode().

Replace field_name with the field in your table

$po_number= DB::table('supplierorders')
                        ->select('supplierorders.*')
                        ->orderby('po_number','asc')
                        ->get()
                        ->map(function($item)
                        {
                            return explode('.', $item->field_name)[0];
                        });
  • Related