I need to format the date to search, since the date that comes from the variable is the example 2021-09-03, without the time and the one from the database comes with the time. I only need to look for the date, not the time.
SaleController.php
public function index()
{
$idUser = Auth::id();
$search = request('calendar');
$sale = Sale::with('client', 'products')->where('user_id', '=', $idUser)->where("DATE_FORMAT(updated_at, '%Y-%m-%d')", '=', $search)->get();
//Here it throws an error because it is misspelled
return $sale;
}
I only need to look for the date, not the time.
You could do this when filtering for date ranges:
$start_date = $request->start_date;
$end_date = $request->end_date;
$sale = Sale::whereBetween('created_at', [$start_date, $end_date])->with('client', 'products')->where('user_id', '=', $idUser)->get()
or this as in your own use case
$search = request('calendar');
$sale = Sale::where('created_at', $search )->with('client', 'products')->where('user_id', '=', $idUser)->first();
To reduce the amount of data loaded for each query, you could refactor your query to:
$search = request('calendar');
$sale = Sale::where('user_id', '=', $idUser)->with('client', 'products')->where('created_at', $search )->first();
Note the use of "first()" instead of "get()" first() returns an object while get() returns an array.
You can use the whereRaw query builder method for raw SQL expressions:
$sale = Sale::with('client', 'products')->where('user_id', '=', $idUser)->whereRaw("DATE_FORMAT(sales.updated_at, '%Y-%m-%d') = '?'", [$search])->get();
See: https://laravel.com/docs/8.x/queries#whereraw-orwhereraw
Related
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
I have the following tables (with only relevant fields):
devices
id
name
created_at
updated_at
device_reports
id
device_id
location
created_at
updated_at
I have a report with a number of filters on it that is already working, so I want to stick with the eloquent way of doing things. Here is Controller function:
public function devices(Request $request)
{
$devicesQuery = Device::with(['latestReport']);
if ($request->ajax())
{
if($request->input('start') && $request->input('start')!='')
{
$start_date = date('Y-m-d', strtotime($request->input('start')));
$end_date = date('Y-m-d', strtotime($request->input('end')));
$devicesQuery = $devicesQuery->lastReportBetween($start_date,$end_date);
}
$devices = $devicesQuery->paginate(10);
return Response::json(View::make('devices/table', array('devices' => $devices))->render());
}
}
The model's latestReport is defined as:
public function latestReport()
{
return $this->hasOne('App\Models\DeviceReport')->latest();
}
The model's function lastReportBetween is defined as:
public function scopeLastReportBetween($query, $start, $end)
{
$query = $query->join('device_reports AS dr', 'dr.device_id', '=', 'devices.id');
$query = $query->where('dr.id', '=', DB::raw('(SELECT max(dr2.id) FROM device_reports AS dr2 WHERE dr2.device_id = devices.id)'));
$query = $query->where(DB::raw("(IFNULL(dr.gps_time, dr.created_at))"), '>=', DB::raw("STR_TO_DATE('".$start."', '%Y-%m-%d')"));
$query = $query->where(DB::raw("(IFNULL(dr.gps_time, dr.created_at))"), '<=', DB::raw("STR_TO_DATE('".$end."', '%Y-%m-%d')"));
return $query;
}
When running the above with a start/end date selected, I get the correct records returned, but I don't get anything returned in "latestReport", but when I run the page without the date filters in place, it correctly returns the device information and the most recent report record in the latestReport class variable.
Can anyone help me understand how to change this code such that I do get the latestReport back when I also call the lastReportBetween function?
I figured out my problem. I should have been using "whereHas()" instead of manual joins and whatnot.
public function scopeLastReportBetween($query, $start, $end)
{
return $query->whereHas('latestReport', function($reportsQuery) use ($start, $end)
{
$reportsQuery->whereBetween('created_at', [$start, $end])
->where('device_reports.id', '=', DB::raw('(SELECT max(dr2.id) FROM device_reports AS dr2 WHERE dr2.device_id = device_reports.device_id)'));
});
}
I want to create a filter using laravel Query Builder.
I have the following code:
$queries = array();
$paginate = 10;
$orders = DB::table('orders')->whereNull('deleted_at');
// the filter i want to apply
$from = 2018-06-18;
$end = 2018-06-20;
$orders->whereBetween('orders.created_at', array($from, $end));
$queries['dateRange'] = request('dateRange');
// other filters
...
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('OrderID', 'desc')
->paginate($paginate)
->appends($queries);
The problem is that i get an error - is not working. If a haven`t used leftjoin i will probably used like this
($orders = Order::query();).
How can i use properly the DB::table queries with some filters, or is another better solution to this?
UPDATE
to be more specific:
this code works:
$orders = DB::table('orders')->whereNull('deleted_at')
->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc')
->paginate($paginate)
->appends($queries);
this code does not work:
$orders = DB::table('orders')->whereNull('deleted_at');
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc')
->paginate($paginate)
->appends($queries);
I have changed to this approach and now it works. The problem was i have to specify on another line the paginate:
$orders = DB::table('orders')->whereNull('deleted_at');
// date filter
$orders->whereBetween(DB::raw('DATE(orders.created_at)'), array($from, $to));
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc');
// the paginate and appends write on new line :)
$orders = $orders->paginate($paginate)->appends($queries);
I'm having trouble retrieving data from the database using Eloquent. I am trying to retrieve all data between two dates (created_at field).
I've done this:
public function search(Request $request){
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('Date_Occured',[$start_date, $end_date])
->get();
dd($getinfo);
}
if you want to select between dates of the media table for example, then fully qualify the field e.g.
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('media.created_at', [$start_date, $end_date]);
->get();
Also make sure the dates passed in the request are in a correct YYYY-mm-dd format
I have this code
use Carbon;
$now = Carbon::now()->toDateString();
So i get the date without the time
public funtion test($brandid){
$nbofsale=DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodate', $now)
->get();
return $nbofsale;
}
The eodate field in the select is datetime. I want to put it in this query as date only and not to change the field type in database.
Any ideas?
If eodate is a timestamp, you don't need to convert it to a date string:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->whereDate('eodata', Carbon::today()->toDateString());
->get();
Another way to get record for today:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodata', '>', Carbon::now()->startOfDay());
->get();