I am trying to understand why the following is always returning false:
public function checkDate($transactions)
{
$start = Carbon::now()->subMonth();
$end = Carbon::now();
$transactions->each(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if ($date->between($start, $end))
{
return true;
}
return false;
});
return false;
}
When I remove both the return false, then null is being displayed.
When I use a regular foreach, it works:
$start = Carbon::now()->subMonth();
$end = Carbon::now();
foreach($transactions as $transaction)
{
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if($date->between($start, $end))
{
return true;
}
return false;
}
The thing what I don't understand is, when I do the following:
public function checkDate($transactions)
{
$start = Carbon::now()->subMonth();
$end = Carbon::now();
$transactions->each(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
if ($date->between($start, $end))
{
dd('test');
return true;
}
return false;
});
return false;
}
Then "test" is being displayed.
Can someone explain as to why this is happening, since I don't understand it.
The each method doesn't stop iterating when true is returned. It does stop, however, when false is returned.
If you would like to stop iterating through the items, you may return false from your callback:
Collection Methods - each()
Your method only returns false also.
If you are trying to verify all transactions fall with a date range, then use the every method:
// returns true if all transactions date are between start and end, otherwise returns false.
return $transactions->every(function ($transaction) use ($start, $end) {
$date = Carbon::createFromFormat('Y-m-d', $transaction->date);
return $date->between($start, $end);
});
Related
this works perfectly fine
return Webinar::query()
->withCount('users')->orderByDesc('users_count')
->get();
but I need to use a condition for sorting, but this doesnt work
return Webinar::query()
->where(function($query) use ($sort_by) {
if($sort_by == 0) {
return $query->withCount('users')->orderByDesc('users_count');
} else {
return $query->withCount('review')->orderByDesc('review_count');
}
})
->get();
I also tried a condition inside like this to check my if else function
return Webinar::query()
->where(function($query) use ($sort_by) {
if($sort_by == 0) {
return $query->withCount('users')->orderByDesc('users_count')
->where('status', 2);
} else {
return $query->withCount('review')->orderByDesc('review_count')
->where('status', 1);
}
})
->get();
but the where status is only working not withcount. I hope someone can answer, thank you so much
How about you move condition code outside query
but I need to use a condition for sorting, but this doesnt work
if ($sort_by === 0) {
$order = 'users_count';
$count = 'users';
} else {
$order = 'review_count';
$count = 'review';
}
return Webinar::query()
->withCount('users')->orderByDesc($order)
->get();
Situation:
$post_1 = 'john';
$post_2 = 30;
$arr = array(['name'=>'john','number'=>70],['name'=>'clark','number'=>50]);
$collection = collect($arr);
if($post_1 == 'john')
{
$collection->where('name',$post_1);
}
if($post_2 == 70)
{
$collection->where('number',$post_2);
}
var_dump($collection->all());
But this doesn't work. I want to include filters but it depends on the post parameters to exist.
I think you can use when
$result= $collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
})->all();
dd($result);
or
$collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
});
dd($collection->all());
Ref:https://laravel.com/docs/8.x/collections#method-when
This code is working. It checks for the exact barcode, case sensitive:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
However, I need to add one more WHERE clause to check if Expiration is greater than today. I tried this but it does not work.
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->where('expiration','>',new Date())
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
This should work:
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$patronbarcode = Patron::where(Patron::raw("BINARY `barcode`"), $findpatronbarcode)
->where('debarred', NULL)
->whereDate('expiration','>', date('Y-m-d'))
->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
PS: as far as I understand, your question isn't about multiple "where" conditions in the request, as you've already achieved that in the working example, instead, it's about making a filter for date values.
Check Laravel 7.x Where Clauses docs(still true for version 5.x and 6.x)
public function findpatronbarcode () {
if ($findpatronbarcode = \Request::get('q')) {
$whereClauses= [[Patron::raw("BINARY `barcode`"), '=', $findpatronbarcode],
['debarred','=', NULL],
['expiration','>', Carbon::now()]];
$patronbarcode = Patron::where($whereClauses)->paginate(10);
}else{
$patronbarcode = ''; //If nothing found, don't return anything.
}
return $patronbarcode;
}
My English is not good so sorry for any mistake. I'm new to Codeigniter, I am trying to search record by using this code, this is my controller code:
public function search() {
if(isset($_POST['search']))
{
$s = $_POST['search'];
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
$data['user'] = null ;
if($res)
{
$data['user'] = $res ;
}
$this->load->view('filter' , $data);
}
}
It is working fine but I want to write this code in separate Model.
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();
But i don't know how to pass this variable value $s = $_POST['search']; to my model. Any suggestion?
you can try the following
//in your application/models Folder put a File called UserSearch_Model.php
class UserSearch_Model extends CI_Model
{
public function search($strSearch)
{
$query = $this->db
->select('u_id,name,email,phone')
->from('user')
->where('name' ,$s)
->get();
return $query->result();
}
}
//and in your controller
public function search()
{
$strSearch = $this->input->post('search');
if (!empty($strSearch))
{
$data = [];
$this->load->model("UserSearch_Model");
$data['user'] = $this->UserSearch_Model->search($strSearch);
if (is_array($data['user']) && count($data['user']) > 0)
{
$this->load->view('filter' , $data);
}
}
}
You can try this solution for your problem :
public function search() {
$search_value = $this->input->post('search'));
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
if (isset($search_value) && !empty($search_value)) {
$this->db->where('name',$search_value);
// OR
$this->db->like('name', $search_value);
}
$query = $this->db->get();
$res = $query->result();
$data['user'] = null;
if ($res) {
$data['user'] = $res;
}
$this->load->view('filter', $data);
}
I hope it will help.
Controller
public function search() {
$search = $this->input->post('search');
$this->load->model('your_model'); //<-- You can load the model into "__construct"
$search_result = $this->your_model->your_search_function($search); //<-- This is how you can pass a variable to the model from controller
$this->load->view('your_view',['search_result'=>$search_result]); //<-- This is how you can load the data to the view. Hear your search result array is loaded on your view
}
Model
public function your_search_function($search) {
//You will receive this search variable ^^ here in your model
$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name',$search);
$query = $this->db->get();
$result = $query->result_array();
if(isset($result) && !empty($result))
{
return $result;
} else
{
return FALSE;
}
}
I use :
DB::connection()->enableQueryLog();
var_dump(DB::getQueryLog());
But it's not working.
My service is like this :
public function dataCustomerList($param)
{
$status = $param['status_sort'];
$gender = $param['gender'];
$published = $param['published'];
if($published=='NO'){
$published_check = 'NO';
$published = 0;
}
else if($published=='YES'){
$published_check = 'YES';
$published = 1;
}
DB::connection()->enableQueryLog();
$customer = customer::paginate(10);
if(!empty($status)) {
// die($status);
$customer = customer::where('tb_customer.customer_status', '=', $status)->paginate(10);
}
if (!empty($gender)) {
$customer = customer::where('tb_customer.gender', '=', $gender)->paginate(10);
}
if (!empty($published_check)) {
$customer = customer::where('tb_customer.published', '=', $published)->paginate(10);
}
var_dump(DB::getQueryLog());
return $customer;
}
When I run it, query does not appear
The result is like this :
array(0) {
}
How to get the query executed in Laravel 5?
Thank you very much
You could listen to query events on DB:
DB::listen(function($query) {
var_dump($query->sql);
});