delphinoy's avatar

Working on SQL but not on Code

Why is this query working on MySQL Workbench but not on Lumen?

Query:

SELECT * FROM domain_apps WHERE date(created_at) = "2021-02-04" and macaddress= "BC-2D-EF-1C-DB-AB";

I'm getting records.

Lumen code:

$results = DB::select( DB::raw('SELECT * FROM apps WHERE date(created_at) = "2021-02-04" and macaddress= "BC-2D-EF-1C-DB-AB"') );

Result is empty.

0 likes
17 replies
jlrdw's avatar

You are mixing query Builder code with DB facade code you don't need the raw in there.

Look straight at the database chapter, the select example.

delphinoy's avatar

I actually tried on Eloquent but same result so I experimented on the Raw.

jlrdw's avatar

Example from docs:

$users = DB::select('select * from users where active = ?', [1]);

Did you import the DB facade at top of controller, did you set up your configuration for the database.

If you do like the example and it doesn't work you have a missed configuration somewhere.

delphinoy's avatar

Did you import the DB facade at top of controller, = Yes

did you set up your configuration for the database. = Yes, I have a lot of function already working but this one is giving me the headache.

If you do like the example and it doesn't work you have a missed configuration somewhere.

I've tried it like this

$results = DB::select( 'SELECT * FROM domain_apps WHERE date(created_at) = "2021-02-04" and macaddress= "BC-2D-EF-1C-DB-AB"');

Still no result.

Originally before the RAW SQL. I've have this:

$results = DB::table('domain_apps')
            ->wheredate('created_at', '2021-02-04')
            ->where('macaddress',  'BC-2D-EF-1C-DB-AB')
            ->get();

Again, no result.

jlrdw's avatar

See if you get results with dd.

$results = DB::table('domain_apps')
            ->wheredate('created_at', '2021-02-04')
            ->where('macaddress',  'BC-2D-EF-1C-DB-AB')
            ->get();

    dd($results);

Or see what you get in the response in the network tab.

Everything looks correct it's as though you need to check your configuration, also did you run PHP artisan config: clear

I would imagine Lumen has that command also, I use laravel.

But suggestion, have you thought about just using Laravel.

delphinoy's avatar

nothing on the dd result.

There is no php artisan config:clear on Lumen only php artisan cache:clear

Yes, I might just have to convert it to full Laravel.

automica's avatar

Try with each WHERE separately and you should then be able to see which is not working. Perhaps the where date behaves differently on lumen?

delphinoy's avatar

It's working individually but not when both. On date, I've tried converting it to Carbon date and still won't work.

automica's avatar

Are you getting the same result back if you use each where separately?

1 like
delphinoy's avatar

Not same result because I'm querying different fields.

automica's avatar

Does the result you are expecting appear in both queries?

MichalOravec's avatar

Different data in different databases, so what do you want? A miracle?

automica's avatar

thats where I was getting to. unless there was a result which matches both where statements you'll get no results, as @delphinoy seemed to be getting.

delphinoy's avatar

I've mentioned different field not different database

@automica asked me to check the result each separately

delphinoy's avatar

Yes, getting the result I want for each but not working when combined although the same query is working on MySQL workbench. Thanks for your time.

jlrdw's avatar

@automica they said it's working with a direct query in workbench. So you would think query in laravel would give same results. @delphinoy try getPdo().

Serkan's avatar

$results = DB::select("SELECT * FROM apps WHERE created_at = ? and macaddress= ?",["2021-02-04","BC-2D-EF-1C-DB-AB"']);

Please or to participate in this conversation.