I have this MYSQL query. I want to select all the words starting with a number. This is the working pure MYSQL query.
SELECT 'seriesid', 'seriesname', 'imagepath', 'views', 'scenes'
FROM `series`
WHERE `seriesname` regexp '^[0-9]+'
Now I want to implement this using Laravel. I made some codes. But not working. Here's my Laravel code for above query.
DB::table('series')->
select('seriesid', 'seriesname', 'imagepath', 'views', 'scenes')->
where('seriesname', 'regexp', '\'^[0-9]+\'')->
get();
What's the error in ths code ? It always returns an empty array []
.
In your original MySQL query, you aren't looking for quotes in the column data; in your Laravel one, you are. Try removing them.
DB::table('series')->
select('seriesid', 'seriesname', 'imagepath', 'views', 'scenes')->
where('seriesname', 'regexp', '^[0-9]+')->
get();
To use regex in laravel you have to use whereRaw(). So change this code
DB::table('series')->select('seriesid', 'seriesname', 'imagepath', 'views', 'scenes')->where('seriesname', 'regexp', '\'^[0-9]+\'')->get();
TO
DB::table('series')->select('seriesid', 'seriesname', 'imagepath', 'views', 'scenes')->whereRaw('seriesname regexp ?', array('\'^[0-9]+\''))->get();
If you are unable to generate the query you need via the fluent interface, feel free to use whereRaw https://laravel.com/docs/5.0/eloquent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With