Here is my code:
$arr = Event::select('user_id', 'start', 'end')
->where('start', '!=', $from)
->whereBetween('start', [$from, $till])
->orWhere(function($query) use ($from, $till) {
$query->where('end', '!=', $till)
->whereBetween('end', [$from, $till]);
})->get();
start value: 2021-07-11 9:00:00
end value: 2021-07-11 15:00:00
$from value: 2021-07-11 10:30:00
$till value: 2021-07-11 13:00:00
I tried with Carbon as well, but the query result is nothing. What is the problem?
Also I tried with:
$arr = Event::where('start', '>', $from)
->where('start', '<', $till)
->get();
Simply not working!
The main code segment has some little flaw with
wherebetween('start', [$from, $till])
Because your start date doesn't lies between your from and till value. That returns nothing actually.
Try with your second solution. With a lil change in it.
$arr = Event::where('start', '<=', $from)
->where('end', '>=', $till)
->get();
Related
I have something like this:
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '<=', $from)
->where('end', '>=', $till)
->get();
This is a nice solution form stackoverflow.
It seems works, but I need the opposite.
In my DB I have this data: start: 2020-09-27 10:00:00 end: 2020-09-27 12:00:00
I have to query that rows where for example:
$start_date(2020-09-27 9:00:00) and $end_date(2020-09-27 11:00:00) where both not in start: and end: Hopefully my question is clear enough.
If I am getting you right this is what you need
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '>=', $till)
->orWhere('end', '<=', $from)
->get();
Laravel ->whereDate doesnt include today.
I have 2 datetime col on my DB (start and end). I use this to query
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', $date)
->whereDate('end', '>', $date)
->get();
The $date variable is a string 2020-03-25 11:23:09 and I have an entry on my db with
start - 2020-03-18 10:44:00
end - 2020-03-25 20:59:00
Question: Why does this query does not fetch that entry. Any advice will be helpful. Thanks.
Because whereDate will change to raw sql with mysql inbuilt-method DATE():
select *
from `blas`
where date(`start`) <= '2020-03-25 11:23:09'
and date(`end`) > '2020-03-25 11:23:09'
So date(2020-03-25 20:59:00) => 2020-03-25 which is less than '2020-03-25 11:23:09'.
You can change your code to
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();
Try this
If you want use WhereDate write your query like.
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-18 10:44:00')->format('Y-m-d'))
->whereDate('end', '>',\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-25 20:59:00')->format('Y-m-d'))
->get();
Or use Simple Where like
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();
How to add "within today" (i.e. From 00:00:00 to 23:59:59) in Laravel ?
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', xxx,xxx)
->count();
If you want to get count of today group then you may use like this.
DB::table('groups')->where('owner_id', $u_id)
->whereDate('created_at', Carbon::today())
->count();
Or as per your requirement
$start = Carbon::now()->startOfDay(); //2019-07-27 00:00:00.000000
$end = Carbon::now()->endOfDay(); //2019-07-27 23:59:59.000000
DB::table('groups')->where('owner_id', $u_id)
->whereBetween('created_at', [$start, $end])->count();
You can use carbon for this, with start of the day and end of the day
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', [ Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])
->count();
Can someone tell me why im getting this error and how to fix it please.
$lastDayPreviousMonth = date("Y-m-d", strtotime("last day of previous month"));
$firstDayPreviousMonth = date("Y-m-d", strtotime("first day of previous month"));
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query, $firstDayPreviousMonth, $lastDayPreviousMonth){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();
Im getting the following error when i run this
Missing argument 2 for App\Http\Models\Employees::App\Http\Models{closure}()
I think it has to do with the firstdaypreviousmonth and lastdaypreviousmonth parameters im passing into the orWhere clause - if i take it out i get undefined variable.
You ca n use closure using use keyword
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query) use($firstDayPreviousMonth, $lastDayPreviousMonth,$clientId){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();
I want to get all the entries of a specific day for a user.. I don't know how to format the query.
public function getEntry()
{
$entries = Journal::where('id', '=', Auth::id())
->where('created_at', '=', '\Carbon\Carbon::now()->format("l j F Y")')
->get()->first();
return view('home')->with(compact('entries'));
}
I do not know how to format that 'created_at' to match the server time. Any help will be largely appreciated. Thank You.
Use whereDate() to get all entries for the specific day:
->whereDate('created_at', Carbon::today());
Or use whereBetween():
->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()])
Or simple where():
->where('created_at', '>', Carbon::now()->startOfDay())
->where('created_at', '<', Carbon::now()->endOfDay())