Getting nothing when doing whereBetween in laravel - laravel

I'm trying to create a sort function where if it's selected it will display the necessary amount of orders. For example if the user selects to display orders from the last 3 months then that needs to be displayed.
The problem I'm having is that nothing is being shown when I dd($three_months)
public function trackOrders()
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contacts = Contact::all();
$orders = Auth::user()->orders->sortByDesc('order_date');
$orders->transform(function($order, $key){
$order->cart = unserialize($order->cart);
return $order;
});
$from = Carbon::now('+2:00');
$to = $from->copy()->subMonth(3);
$three_months = Order::whereBetween('created_at', [$from, $to])->get();
dd($three_months);
return view('public.users.track-orders', compact('menus_child', 'contacts', 'orders', 'order_item'));
}
but when I do dd($three_months) nothing shows up. I only get
Collection {#320 ▼
#items: []
}

Order matters when using SQL's BETWEEN. Your $from value is greater than your $to value. So try swapping them around:
$to = Carbon::now('+2:00');
$from = $from->copy()->subMonth(3);
$three_months = Order::whereBetween('created_at', [$from, $to])->get();

Maybe it's because you are not Formatting DateTime
$from = Carbon::now('+2:00')->format('Y-m-d H:i:s');
$to = $from->copy()->subMonth(3)->format('Y-m-d H:i:s');
Try This code.
Edit: You cant Use copy() method on string. so you can do.
$to = Carbon::now('+2:00')->subMonth(3)->format('Y-m-d H:i:s');

Related

Foreach loop is showing error while storing multiple id's

I am creating a group and also storing users id's in it but its showing error in foreach loop i.e. Invalid argument supplied for foreach().
Here is my controller code :
public function createGroup(Request $request)
{
$user_id = request('user_id');
$member = request('member');
$data = array(
'name'=>$request->name,
);
$group = Group::create($data);
if($group->id)
{
$resultarr = array();
foreach($member as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}
}
I am adding values like this,
Desired Output,
I just want that each member to store with different id's in table and group id will be same.
Please help me out
Avoid that if check, it does absolute nothing.
if($group->id)
Secondly your input is clearly a string, explode it and you will have the expected results. Secondly don't save it to a temporary variable, create a new GroupUser immediately.
foreach(explode(',', $member) as $data){
$addmem = new GroupUser();
$addmem->user_id = $data;
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
}
That implode line makes no sense at all, i assumed there is a user_id on the GroupUser relation.
u need to send array from postman
like
Key | value
member[] | 6
member[] | 3
or
$memberArray = explode(",", $member = request('member'))
if($group->id)
{
$resultarr = array();
foreach($memberArray as $data){
$resultarr[] = $data['id'];
}
$addmem = new GroupUser();
$addmem->implode(',', $resultarr);
$addmem->group_id = $group->id;
$addmem->status = 0;
$addmem->save();
return $this->sendSuccessResponse([
'message'=>ResponseMessage::statusResponses(ResponseMessage::_STATUS_GROUP_SUCCESS)
]);
}

laravel change url and id on each loop

I have something like this:
function odeon(){
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
$id = 1;
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
$business_id = 1;
$film = Film::where('id', $id)->first();
$id = $id + 1;
// if news is null
if (!$film) {
$film = new Film();
}
$film->title = $title;
$film->times = $time;
$film->business_id = $business_id;
$film->save();
}
return view('odeon')->with(['listings' => $data->listings]);
}
What I want to do is add more cinemas so instead of:
$data=
file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/6756');
Be something like:
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/6756-10565');
So to go throught from numbers 6756 to 10565, problem is this as well as each cinema has a different business_id I don't want to replicate function for each cinema so any help will be appreciated ;)
Another issue is what if another day therewill be less films than previous day, of course I don't want to display yesturdays films for today
//edit
Ok I will do my best to explain what I want to achieve.
I want to loop throught a cinema, retrieve films for today, save into database, retrieve data for tomorrow save into database, etc. I want to do it for 7 days, Now for each cinema, cinema id + date changes:
https://api.cinelist.co.uk/get/times/cinema/10565?day=2017-06-14
Would bring me back films for today, but I want to achieve it for 7 days so something like this:
$cinema_id=['10', '20'] (number = cinema_id)
$startdate = strtotime("today");
$enddate = strtotime("+7 Days");
while ($startdate < $enddate) {
echo date("Y-m-d", $startdate) . "<br>";
$startdate = strtotime("+1 Days", $startdate);
}
$data= file_get_contents (https://api.cinelist.co.uk/get/times/cinema/$cinema_id?day=$startdate)
So basically I want to loop throught array of cinema and for each cinema_id, go throught 7 days ahead and save details to database. Of course I don't want to keep old records, so each day they either have to be removed or updated
Your question is vague but probably you mean this...
function odeon(){
$arrayOfCinemaIds = [10565,6756];
foreach($arrayOfCinemaIds as $id){
//append your id
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id);
$data = json_decode($data);
$id = 1;
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
$business_id = 1;
$film = Film::where('id', $id)->first();
$id = $id + 1;
// if news is null
if (!$film) {
$film = new Film();
}
$film->title = $title;
$film->times = $time;
$film->business_id = $business_id;
$film->save();
}
$listings[]=$data->listings;
}
//$listings will have all the listings from all the loops
return view('odeon')->with(['listings' => $listings]);
}
You will just need to add more entries to $arrayOfCimenaIds as you need. Typically you would load them up from a Db or other data store.
EDIT:
If you have a sequence of numbers you dont need the array you can do
for($id=6756;$id<=10565;$id++){..}
instead of the foreach()

laravel retrieve json and save into database

I am getting cinema title + times using API from Cinelist, I then want to save these values into a database.
At the moment, it does save but only 1 record, the last one. However, I want it to save each one.
Also each time it is run I want to update existing records instead of creating new ones unless there are more results.
So usually there are 5 records, each time I run the function I want to update the database with the new 5 records, however, if it's a different day and there are 6 records I want to update 5 records and insert 1 extra one so there is 6.
My code so far:
function odeon(){
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
$id = + 1;
$films = Film::where('id', $id)->first();
if (!$films) {
$films = new Film();
}
$films->title = $title;
$films->times = $time;
$films->save();
}
}
You may use eloquent's updateOrCreate method to insert non-existent data and update existing data.
function odeon(){
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
foreach($data->listings as $listing){
$title = $listing->title;
$time = implode(', ', $listing->times);
Films::updateOrCreate([
'title' => $title,
'$times' => $time
]);
}
}

codeigniter get array data in controller

I have sql query in controller, this query produces number of rows.
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
then i pass this data to my model,
public function update_cancel_stock($code,$qty)
{
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $order );
$this->db->update("tbl_inventory6");
}
but no any update. please check above code. thanx
Try this
$cond = array("order_id" => $order_no, "location" => $location);
$q1 = $this->db->select("product_code,product_quantity")->from("tbl_order_details")->where($cond)->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
and then model ($code was not used)
public function update_cancel_stock($code,$qty){
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $code);
$this->db->update('tbl_inventory6');
}
You are using row() function of Active Record. It already return only one row. So there is no requirement of foreach. Try this:
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
If you are getting multiple records and you are using foreach, you should use result().
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->result();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}

Codeigniter: Can I return multiple values from the same function?

I would like to return the query results along w/ the row count without having to run multiple queries. Is this possible in Codeigniter? The following works for returning the query results, but is it possible to also count the number of entries found?
Controller:
$data['records'] = $this->item_model->searchItem($item_name);
Model:
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query->result();
$bindings = array($this->user_id, '%'.$item_name.'%');
$records = $this->db->query($sql, $bindings)->result();
return array(
'records' => $records,
'count' => count($records),
);
Then, in your controller:
$query = $this->item_model->searchItem($item_name);
$data['records'] = $query['records'];
$data['count'] = $query['count'];
Option one
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
$data['result'] = $query->result();
$data['rows'] = $query->num_rows();
return $data;
Option two
// model
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query;
// controller
$data['query'] = $this->item_model->searchItem($item_name);
// then you have $data['query']->result();
// and $data['query']->num_rows();
You can send as many variables from a function as you want ..However plz remember that a function is supposed to do one UNIT of work . thus this rule must not be violated.
we can used contrel structure
if this {return A;} elseif () {return B;} else {return C;}
ALso we can send (bundle) variables in an array and send

Resources