Laravel whereDate not working - php

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

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 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: Get all records where expires_at is in the past?

I have a variable where I get the records from GameMute modal and I need to know how I can only get the ones where expires_at is not yet expired. Its formatted just like created_at
$mutes = GameMute::orderBy('created_at', 'asc')->get();
Looked all over google but its with formatted time or something else.
Thanks.
Use Carbon like this,
$mutes = GameMute::where('expires_at','>', \Carbon\Carbon::now())
->orderBy('created_at', 'asc')->get();
Try this one :
$date = date("Y-m-d h:i:s");
$users = DB::table('users')->whereDate('expires_at',">",$date))
->orderBy('created_at', 'asc')->get();
Try where condition
$mutes = GameMute::whereRaw('expires_at > now()')->orderBy('created_at', 'asc')->get();
Try these
$mutes = GameMute::where('expires_at ', '>', new DateTime('today'))->orderBy('created_at', 'asc')->get();
Hope your expire_at column is a date type column
$mutes = GameMute::where('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();
Or if it contained date time value like "2017-07-31 16:50" then you can query like this :
$mutes = GameMute::whereDate('expires_at','>',date('Y-m-d'))
->orderBy('created_at', 'asc')
->get();

Laravel SQL select query where DateTimestamp contains this month and year

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.*');

Using Carbon for Laravel queries

how can I use Carbon to output created_at human readable queries like Facebook's 1 min ago on it's posts. The problem is I always return the results of the query to output in the browser directly. How can I integrate it so instead of 2015-07-01 12:32:43 it would be 1 min ago or other that's human readable and nice.
In my controller:
$posts = DB::table('posts')
->where('author_id', '=', $id)
->where('created_at', '<', $date)
->orderBy('created_at', 'desc')
->paginate(10);
return $posts;
It will then return a json format of all posts.
I recommend you use an Eloquent model and the features that come with it:
class Post extends Eloquent {
protected $appends = array('created_at_for_humans');
public function getCreatedAtForHumansAttribute(){
return Carbon::parse($this->attributes['created_at'])->diffForHumans();
}
}
This attribute accessor returns the formatted date and because it is registered in $appends the attribute will be included for array/JSON conversion.
And your query would look nearly the same:
$posts = Post::where('author_id', '=', $id)
->where('created_at', '<', $date)
->orderBy('created_at', 'desc')
->paginate(10);
Try:
foreach ($posts as &$post) {
$post->created_at = Carbon::parse($post->created_at)->diffForHumans();
}
before return

Categories