Laravel equivalent query builder - php

How to convert below query into Laravel equivalent query builder
SELECT * FROM `table_name` WHERE '03-05-2017 09:30' BETWEEN `start_date` AND `end_date` AND `who_should`='VV000'
I tried using whereBetween but not working as expected.

$given_time = "03-05-2017 09:30";
\DB::table('table_name')
->whereRaw(" '$given_time' Between start_date and end_date ")
->where("who_should", "=", "VV000")
->get();

$date = new Carbon\Carbon('03-05-2017 09:30');
$date_string=$date->toDateTimeString();
$data_set = DB::table('table_name')
->select(DB::raw('*'))
->where('start_date', '<', $date_string )
->where('end_date', '>', $date_string )
->where('who_should','=','VV000')
->get();
Try above code

Related

How to transform sql query into Query Builder Laravel way?

I need to convert this query into Laravel query builder or ORM
SET #start_date = '2020-11-01';
SET #end_date = '2020-11-08';
SET #duration = CONVERT(#end_date, DATE) - CONVERT(#start_date, DATE);
SELECT item_id, days
FROM (
SELECT item_id, sum(end_date - start_date) AS days
FROM schedule WHERE start_date >= #start_date AND end_date <= #end_date
GROUP BY item_id) AS virtual
WHERE days = #duration;
(i use Laravel 8)
i could not find similar example I could analize and try by myself :(
i try this :
$res = DB::table('schedule')
->select('schedule.item_id' , DB::raw("SUM(schedule.end_date - schedule.start_date) as days"))
->where('start_date', '>=', $start_date)
->where('end_date', '<=', $end_date)
->groupBy('item_id')
->where('days', '=', $duration)
->get();
but i get error :
Column not found: 1054 Unknown column 'days' in 'where clause'
ok i know what is wrong
i tried to access 'days' column before it is created with AS
that column will be available after line ->get() is executed.
So I changed order of these 2 lines :
->where('days', '=', $duration)
->get();
to
->get()
->where('days', '=', $duration);
and now works :)

Laravel datetime created_at

I have a question. If created_at in my database is datetime and I want to make a query like this
whereDate('created_at', '=', date('Y-m-d'))
I get nothing as a result. How can i handle this situation ?
Another case: I want
whereDate('created_at', '=', date('M'))
You can use other helper functions maybe:
$query->whereYear('created_at', '=', date('Y'))
->whereMonth('created_at', '=', date('m'))
->whereDay('created_at', '=', date('d'));
Assuming you are using MySQL, you could use whereRaw:
whereRaw('created_at = CURDATE()')
For your second case:
whereRaw("month_name = DATE_FORMAT(CURDATE(), '%b')")
First Query: You can use Carbon specifying date format (date without time toDateString() / date with time toDateTimeString() ).
$query->whereDate('created_at', '=', Carbon::today()->toDateTimeString());
Second Query: Just query on whereMonth
$query->whereMonth('created_at', '=', date('m'));

curdate() query not working in laravel 5.2

I have one problem in my query is CURDATE() not working in my laravel 5.2
AbsenController#index
$now = time();
$absen = Absen::with('siswa')->where('level', '=', 'Siswa', 'AND',
'created_at', '<=', 'CURDATE()')->get();
return view('absen.index')->with('data', $absen);
and this is record in my Absen Table
I am not familiar with your where sytnax. Try including separate terms for each of the two conditions in your WHERE clause:
$absen = Absen::with('siswa')
->where('level', '=', 'Siswa')
->where('created_at', '<=', DB::raw('curdate()'))
->get();
As a variant, you could also use whereRaw() to handle the condition involving CURDATE():
$absen = Absen::with('siswa')
->where('level', '=', 'Siswa')
->whereRaw('created_at <= curdate()')
->get();
Conditions are ANDed together by default, which is the relationship you have between your two conditions. Look into orWhere() if you want to OR together conditions in the WHERE clause.
$now = time();
$absen = Absen::with('siswa')->where('level', 'Siswa')->where('created_at','<=',\Carbon\Carbon::now())->get();
return view('absen.index')->with('data', $absen);

Find By Date Laravel 5.2

I have some problem with searching by existing date.
In Model:
$date = '2016-01-14';
public static function getByDate($date)
{
$query = self::select('date', DB::raw('SUM(test_count) as test'))
->whereDate('date', '=', $date)
->groupBy('date')
->orderBy('date', 'ASC')
->get();
}
But I didn't get a result, because this code creates SQL:
select `date`, SUM(test_count) as test from `test_table` where date(`date`) = 2016-01-14 group by `date` order by `date` asc limit 1
Not quoted around of date => 2016-01-14
How to fix it?
I try to use whereRaw with params, for example:
return $query->whereRaw("date= ?",[$date]);
but it did not help...
You need to change the ->whereDate('date', '=', $date) to this:
->where('date', '=', $date)
Or you can use this:
->whereDate($date)
So, it's obvious that, you can use either ->whereDate($date) or ->where('date', '=', $date) because whereDate is a dynamic method and in this case, it'll become: where('date', '=', $date).

Laravel Sqlcommand could not retrive result

In phpmyadmin MySQL command is ok and that can be fetch result correctly. After using this command with QueryBuilder result set is empty.
MySQL Command :
SELECT created_at
FROM `entryExit`
WHERE `userId` =1000
AND `insertDate` = DATE( CURDATE( ) )
LIMIT 0 , 30
Result:
created_at
2014-06-30 05:53:18
laravel QB:
$checkSave = DB::table('entryExit')
->select(DB::raw('created_at'))
->where('userId', '=', Auth::user()->userId)
->where('insertDate', '=', 'DATE( CURDATE() )')
->get();
Result:
Array
(
)
try changing:
...
->where('insertDate', '=', 'DATE( CURDATE() )')
...
to
...
->where('insertDate', '=', DB::raw('DATE(CURDATE())'))
...

Categories