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
Related
I have a export method in Laravel 5.8 for exporting some results from oders table:
public static function getAllData()
{
$records = DB::table('orders as t1')
->leftjoin('payments as t2', 't1.ord_id', '=', 't2.pay_ord_id')
->leftjoin('members as t3', 't2.pay_creator_id', '=', 't3.mbr_usr_id')
->where('t2.pay_confirm', 1)
->where('t1.ord_status','completed')
->orWhere('t1.ord_status','processing')
->select('t1.ord_id','t1.ord_date','t1.ord_total','t3.mbr_mobile','t3.mbr_name','t3.mbr_family')
->get();
return $records;
}
So it works fine and clean but I do need to add one more column for checking if the order payment type is by wallet or paid directly.
So every payment has a pay_type_id and if it is set to 189, then it should print Paid With Wallet otherwise print Paid Online as the appended column.
But the problem is, I don't know how to check this if..else condition in this method...
So if you know, please let me know.
Thanks.
you could use case to do if else condition
for example like this
public static function getAllData()
{
$records = DB::table('orders as t1')
->leftjoin('payments as t2', 't1.ord_id', '=', 't2.pay_ord_id')
->leftjoin('members as t3', 't2.pay_creator_id', '=', 't3.mbr_usr_id')
->where('t2.pay_confirm', 1)
->where('t1.ord_status', 'completed')
->orWhere('t1.ord_status', 'processing')
->select(DB::raw("case when pay_type_id=189 then 'Paid With Wallet' else 'Paid Online' end as your_column_name"),'t1.ord_id', 't1.ord_date', 't1.ord_total', 't3.mbr_mobile', 't3.mbr_name', 't3.mbr_family')
->get();
return $records;
}
Try
->select(DB::raw('IF(pay_type_id=189,"Paid With Wallet","Paid Online as") as order_payment_type'))
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();
...
I try to combine whereNotExists and where, if you use it simultaneously it will and if you try one of them you can
$query = DB::table('tabel_produk')->where('kode_customer',$cust)->whereNotExists(function($query){
$query->select(DB::raw(1))->from('tabel_detail_realisasi')->whereRaw('tabel_detail_realisasi.barcode = tabel_produk.barcode');
})->distinct()->orderBy('barcode','asc') ->get();
Pleae help me with this
I don't have your DB schema so I've tried something similar with a local DB I have and it seems to be working fine. See my example below. Could you please replace my query with yours and see what's output by the query logger?
DB::flushQueryLog();
DB::enableQueryLog();
$results = DB::table('product_templates')
//Non deleted products
->where('deleted', '=', '0')
//
->whereNotExists(function ($query) {
//Where the category is not 'General'
$query
->select(DB::raw(1))
->from('product_categories')
->whereRaw('product_categories.id = product_templates.category_id')
->where('product_categories.name', 'General');
})
->distinct()
->orderBy('name', 'asc')->get();
DB::disableQueryLog();
$queryLog = DB::getQueryLog();
logger("Query results", compact('results', 'queryLog'));
I want to filter my query and return only if the user hasRole "Premium" and limit the result to 10.
Along with this query is a count of records which Conversion has and sort it in DESC order by total column.
Right now I have a working query that returns the count of Conversion and with a User but without user filter Role.
// Model Conversion belongs to User
// $from & $end uses Carbon::createDate()
// Current code
$query = Conversion::select('user_id',DB::raw('COUNT(*) as total'))
->whereBetween('created_at', [$from,$end])
->where('type','code')
->where('action','generate')
->whereNotNull('parent_id')
->with('user')
->groupBy('user_id')
->orderBy('total', 'DESC')
->take(10)
->get();
// current result
foreach ($query as $q) {
$q->user->name; // To access user's name
$q->total; // To access total count
}
// I tried this but no luck
$query = Conversion::select('user_id',DB::raw('COUNT(*) as total'))
->whereBetween('created_at', [$from,$end])
->where('type','code')
->where('action','generate')
->whereNotNull('parent_id')
->with('user', function($q) {
$q->hasRole('Premium');
})
->groupBy('user_id')
->orderBy('total', 'DESC')
->take(10)
->get();
You need to use whereHas instead of with, like this:
->whereHas('user', function ($query) {
$query->where('role','Premium');
})
Use the whereHas() instead of with(). Also, you can't use hasRole() if it's not a local scope:
->whereHas('user.roles', function($q) {
$q->where('name', 'Premium');
})
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();