Raw Sql to Laravel query builder
This is my raw Sql running properly in my sql.
SELECT * FROM `created_tags` WHERE `expire_date`= DATE(SYSDATE())+INTERVAL '22' DAY AND `is_registered`='1';
I want to convert this into laravel query builder, Please Help
I tried doing this below but it is wrong
DB::table('created_tags')->where('is_registered', '1')
->whereRaw('expire_date = DATE(SYSDATE())+INTERVAL 22 DAY'])
->get();
Hi i personally use carbon for this
Your query should look like
DB::table('created_tags')->where('is_registered', '1')
->where('expire_date', Carbon::now()->addDays(22)->toDateTimeString())
->get();
You need to add use carbon to the top of your file
use Carbon\Carbon;
i would personally also rewrite this to use the model for the table and the query would look like:
CreatedTags::where('is_registered', '1')
->where('expire_date', Carbon::now()->addDays(22)->toDateTimeString())
->get();
@Ploeter
my expire_date is ='2022-09-01'
and carbon::now returns i think the time stamp
This time format doesn't match i think
@SurajKumar query builder has a nice little function called ->whereDate('field', Carbon::now()), which helps with your formatting doubts
If you got your answer mark it as "solved" :)
Please or to participate in this conversation.