Laravel SQL select query where DateTimestamp contains this month and year - php

I'm trying to make a SQL query where I can check if the DateTimestamp contains this month and year.
A part of the query:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->where('invoices.updated_at', '=', date('m-Y')) //Check if DateTimestamp(updated_at) contains Year and month
->select('leads.*')
I tried it with date_parse, new DateTime(), but it didn't work. What am I doing wrong?

You can use whereMonth() and whereYear() to achieve this:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->whereMonth('invoices.updated_at', '=', date('m')) // changed
->whereYear('invoices.updated_at', '=', date('Y')) // changed
->select('leads.*');

Related

How to get data filter date according to field table from database

my code in LaporanController.php
class LaporanController extends Controller
{
public function index(Request $request)
{
if (isset($request->start_date) && isset($request->end_date)) {
$start_date = Carbon::parse($request->start_date)->format('Y-m-d');
$end_date = Carbon::parse($request->end_date)->format('Y-m-d');
$attendance_absent = DB::table('attendance_absent as absent')
->whereBetween('absent.do_date_start', 'absent.do_date_end', [$start_date, $end_date])
->get();
dd($attendance_absent);
}
}
}
how to get request data from start_date and end_date according to attendance_absent table from database fields do_date_start and do_date_end? i try to use whereBetween but i get error : Illuminate\Database\Query\Builder::whereBetween(): Argument #2 ($values) must be of type array, string given. how to solve my problem ?
normally whereBetween using for single column date check. but in this case youu need get from the different columns. so try to check those date like this. i think it will we help full for you.
do try it like this
->whereDate('do_date_start', '>=', $from_date)
->whereDate('do_date_start', '<=', $to_date)
->whereDate('do_date_end', '>=', $from_date)
->whereDate('do_date_end', '<=', $to_date)
other thin if you used whereBetween you will not get equal date of today.
whereBetween function is used to query one field with 2 or more values, what you really want is just where function twice, try this:
...
->where([
['absent.do_date_start', '>=', $start_date],
['absent.do_date_end', '<=', $end_date],
])
->orWhere(function ($query) use ($start_date, $end_date) {
$query->where([
['absent.do_date_start', '>', $start_date],
['absent.do_date_start', '>', $end_date],
['absent.do_date_end', '<', $start_date],
['absent.do_date_end', '<', $end_date],
])
})
->get();
...

Laravel sum from relationship with condition

I have the following query where I have a conditional query with relation between two models. I need sum of column hours from the attendance where the conditions are met. I have tried the following, but it's not working.
$users = User::with('attendance')
->whereHas('attendance', function (Builder $query) use ($end, $start) {
$query->whereBetween('date', [$start, $end])
->where('status', 2)
->select(DB::raw('SUM(hours) as h'));
})->orderBy('name')
->where('status', 0)
->get();
In my blade
#foreach($users as $user)
{{ $user->h }}
#endforeach
Please help
User::whereHas('attendance')
->withSum(['attendance' => function ($query) use ($start, $end){
$query->whereBetween('date', [$start, $end])
->where('status', 2);
}], 'hours')
->get();
you can access sum hours using attendance_sum_hours property
({relation}_{function}_{column})
$user->attendance_sum_hours
Tip:
one more thing; $query->whereBetween('date', [$start, $end]) be carefull when using whereBetween on datetime column because will compare also the time, so the results won't be favorable
use whereBetweenColumn('date(date)', [$start, $end])
I do not think you can do this with whereHas, but this is how you would do it using joins. This would return all users that have an attendance between 2 dates and then give you the count.
$users = User::with('attendances')
->selectRaw('users.*, SUM(attendances.hours) as hours_sum')
->leftJoin('attendances', 'users.id', 'attendances.user_id')
->whereBetween('attendances.date', [$startDate, $endDate])
->groupBy('users.id')
->get();
However, if you want to return all users but if they have no attendance it will return 0 you can do the following
$users = User::with('attendances')
->selectRaw('users.*, COALESCE(SUM(attendances.hours), 0) as hours_sum')
->leftJoin('attendances', function (JoinClause $joinClause) {
$joinClause->on('users.id', 'attendances.user_id')
->whereBetween('attendances.date', [$startDate, $endDate]);
})
->groupBy('users.id')
->get();

laravel 6 search query with multiple conditions

I have a search form with four fields of customer email, vendor name, status, and fromdate, todate,. My search filter is not working properly. I want if someone searches a lead with vendor name and status Active then it shows only leads of that vendor with status active status but here it shows a leads with both Accept or Reject also my date filter is not working so please help me. Please guide me
my controller code is
public function search(Request $request){
$users = DB::table('users')->where('is_admin', Null)->get();
$customer_email = $request->input('customer_email');
$vendor_id = $request->input('vendor_id');
$status = $request->input('lead_status');
$leads = DB::table('leads')
->leftJoin('users', 'leads.vendor_id', '=', 'users.id')
->select('leads.*', 'users.name')
->where('vendor_id', $vendor_id)
->orWhere('customer_email', $customer_email)
->orWhere('lead_status', $status)
->orWhere('leads.created_at', array($request->start_date, $request->end_date))
->orderBy('leads.created_at', 'DESC')->get();
//dd($leads);
return view('admin.view-leads', compact('leads'), compact('users'));
}
please help.
Thanks in advance
query image
That's a pretty simple thing. Just follow the code below.
To get the users where is_admin is null.
$users = DB::table('users')->whereIsNull('is_admin')->get();
To check if you are submitting a value for any filter.
if($request->filled('name_of_filter_input')){ //code goes here }
So your query to filter records will go like.
$query = DB::table('leads')->query();
$query->leftJoin('users', 'leads.vendor_id', '=', 'users.id');
$query->select('leads.*', 'users.name');
if($request->filled('vendor_id')) {
$query->where('vendor_id', $request->input('vendor_id'));
}
if($request->filled('customer_email')) {
$query->where('customer_email', $request->input('customer_email'));
}
if($request->filled('lead_status')) {
$query->orWhere('lead_status', $request->input('lead_status'));
}
if($request->filled('start_date') && $request->filled('end_date')) {
$query->whereBetween(DB::raw('date(leads.created_at)'), [$request->input('start_date'), $request->input('end_date')]);
}
$leads = $query->orderBy('leads.id', 'DESC')->get();
Further you can replace DB::table('table_name') syntax with respective model classes in case query() seems to be undefined function.
Wrap leads.created_at with date() function and ensure your date filter(s) has date in yyyy-mm-dd format if the created_at column is of type timestamp.
you should write your query like as below
$leads = DB::table('leads')
->leftJoin('users', 'leads.vendor_id', '=', 'users.id')
->select('leads.*', 'users.name')
->where('vendor_id', $vendor_id)
->when($customer_email, function($q,$customer_email){
$q->where('customer_email', $customer_email);
})
->when($status, function($q,$status){
$q->where('lead_status', $status);
})
->where(function($q) use($request){
if(isset($request->start_date) && isset($request->end_date)){
$fromDate = date('Y-m-d H:i:s',strtotime($request->start_date));
$toDate= date('Y-m-d H:i:s',strtotime($request->end_date));
$q->where('leads.created_at', '>=', $fromDate)
->where('leads.created_at', '<=', $toDate);
}
})
->orderBy('leads.created_at', 'DESC')->get();
I updated my answer please look into it

Laravel whereDate not working

I want to load more links of day, but the whereDate not working, and I don't know why... The date format is correct.
public function day_load_more($clicks, $total_links, $data) {
$data_carbon = Carbon::createFromFormat('d-m-Y h:i:s', '20-02-2018 00:00:00');
$ex_link_in = explode(',', $_POST['links_inserts']);
$links = Link::where('status', '=', 1)
->WhereNotIn('id', $ex_link_in)
->whereDate('created_at', $data_carbon)
->where('clicks', '<=', $clicks)
->orderBy('clicks', 'desc')
->with('page', 'tag')
->where('sponsored', 0)
->take(10)
->get();
}
The stranger thing, is that in the other method works fine (show only links of day):
public function linksofday($data){
$data_carbon = Carbon::createFromFormat('d-m-Y h:i:s', '20-02-2018 00:00:00');
$links = Link::where('status', '=', 1)
->orderBy('clicks', 'desc')
->with('page', 'tag')
->where('sponsored', 0)
->whereDate('created_at', $data_carbon)
->whereNotIn('id', [$this->getFirstLinkDay($data)->id])
->take(10)
->get();
}
I got 5 links of day 20-02, when I roll the page, should not show anything more, but shows links of others days...
I don't think the issue is with your parsing of the date but rather with some other where or maybe because some of the input variable has wrong data, also instead of using $_POST you can just access it's data with Request object

Search with date range in laravel

I have a db table dateprices
id|tour_id|start|end|price
I want to get all the price of the tour starting and ending between the dates i.e. 2017-06-20 and 2017-07-11
I have written following query but it is returning an empty collection:
$dates = DatePrice::where('tour_id','=',$request->product_id)
->where('start', 'like', '%'.$request->start.'%')
->where('end', 'like', '%'.$request->end.'%')
->get();
Is there something wrong with my query ?
You should use the whereBetween() function like so:
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('start', array('2017-06-20', '2017-07-11'))
->whereBetween('end', array('2017-06-20', '2017-07-11'))
->get();
Try with below query,
If there is a single condition then you can user whereBetween().
If you would like to add more condition, you can use orWhereBetween().
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('start', 'like', '%'.$request->start.'%')
->orWhereBetween('end', 'like', '%'.$request->end.'%')
->get();
You can search by specifying the name of the column (dates) like so;
$dates = DatePrice::where('tour_id','=',$request->product_id)
->whereBetween('date', array($request->from_date, $request->to_date))
->get();

Categories