I have a parsing problem where I want to get all of the people with a particular subscription but not people who have another type of subscription. The subscriptions are stored in a comma delineated list in the subscriptions table in the subscriptions column. Here is the code I have so far:
$includeQuery = [];
foreach ($includeSegment as $include) {
$singleQuery = ['subscriptions','like', '%'.$include.'%', 'or'];
array_push($includeQuery, $singleQuery);
}
$excludeQuery = [];
foreach ($excludeSegment as $exclude) {
$singleQuery = ['subscriptions', 'not like', '%'.$exclude.'%', 'or'];
array_push($excludeQuery, $singleQuery);
}
$included = Subscription::where($excludeQuery)->where($includeQuery)->get();
I get results back but some of them have the excluded subscriptions in them.
use whereIn and whereNotIn instead :
Subscription::whereIn($includeSegment)->whereNotIn($excludeSegment)->get();
then you dont need to also iterate segments once they are arrays of strings
The problem with code above was in the boolean parameter I had "or" instead of "and". So only if all of the subscriptions for a particular user were present would the record get thrown out. Here is the updated code:
$includeQuery = [];
foreach ($includeSegment as $include) {
$singleQuery = ['subscriptions','like', '%'.$include.'%', 'or'];
array_push($includeQuery, $singleQuery);
}
$excludeQuery = [];
foreach ($excludeSegment as $exclude) {
$singleQuery = ['subscriptions', 'not like', '%'.$exclude.'%', 'and'];
array_push($excludeQuery, $singleQuery);
}
// $included = Subscription::where($excludeQuery)->where($includeQuery)->get();
$included = DB::table('subscriptions')
->join('app_users', 'app_users.customer_number', '=', 'subscriptions.customer_number')
->join('app_datas', 'app_datas.customer_number', '=', 'subscriptions.customer_number')
->where($includeQuery)
->where($excludeQuery)
->select('app_datas.device_token')
->get();
Related
I am trying to sort eloquent collection using sortByDesc. It works for first sorting but chaining with more columns doesn't give proper result. Here is the query and code I am trying
$data = Divrank::where('division_id', $id)->with(['team.player1', 'team.player2'])->with('meta')->orderBy('position', 'asc')->get();
foreach($data as $k=>$t){
if ($t->meta) { // it has ranks informations
$t->totalSets = $t->meta->totalSets;
$t->totalGames = $t->meta->totalGames;
$t->points = $t->meta->points;
} else {
$t->totalSets = 0;
$t->totalGames = 0;
$t->points = 0;
}
unset($data[$k]->meta);
}
//return $data;
return $data->sortByDesc('points')->sortByDesc('totalSets')->sortByDesc('totalGames');
If I remove ->sortByDesc('totalSets')->sortByDesc('totalGames') then it shows correct formate.
Any ideas how to achieve this? Thank you.
Ok, got it, I need to do reverse sorting. $data->sortByDesc('totalGames')->sortByDesc('totalSets')->sortByDesc('points'); this works. It will sort first with games, then sets and then points..
I have query something like this:
Order::where('order_id', 2)
->leftJoin('test', 'test.order_id', '=', 'orders.order_id')
->get(['order.status, 'order.path', 'test.name']);
Now, what I want to achieve is the following. in get clause, when fetching specific columns, if 'order.status' isn't COMPLETED, return 'order.path' as null, if 'order.status' is COMPLETED, then return actual order.path
You should try to loop the result, then apply the change what you want.
$orders = Order::where('order_id', 2)
->leftJoin('test', 'test.order_id', '=', 'orders.order_id')
->get(['order.status, 'order.path', 'test.name']);
for($i = 0; i < orders.lenght();i++){
if(orders[i].status != 'COMPLETED'){
orders[i].path = null;
}
}
I'm stuck on this from a while.Can't figured it out.I reed documantion, tried with several videos and tried like 10 different ways, nothing is working yet.So I have one view/model for one thing, in this example Destination and I have separate files for Offers.The controllers for both are in one file.I want tho the information that is in destination to go to Offers as well.Please help I can't figure out what I'm missing:
So here is the most important parts:
destination_model.php
<?php class Destination_model extends CI_Model
{
public function getDestinationDetails($slug) {
$this->db->select('
id,
title,
information
');
$this->db->where('slug', $slug);
$query = $this->db->get('destinations')->row();
if(count($query) > 0)
{
return $query;
}
else
{
// redirect(base_url());
}
}
public function getOffersByDestination($destination_id)
{
$this->db->select('
o.short_title,
o.price,
o.currency,
o.information,
o.long_title_slug,
oi.image,
c.slug as parent_category
');
$this->db->from('offers o');
$this->db->join('offers_images oi', 'oi.offer_id = o.id', 'left');
$this->db->join('categories c', 'c.id = o.category');
$this->db->group_by('o.id');
$this->db->where('o.destination', $destination_id);
$this->db->where('o.active', '1');
return $this->db->get();
} }
And then in the controller for offers I put this:
$this->load->model('frontend/destination_model');
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
All I need is the title and the information about the destination.
Here is the whole controller for the offers:
$data = $this->slugs_model->getOfferDetails(strtok($this->uri->segment(2), "."));
$this->load->model('frontend/offers_model');
$this->load->model('frontend/destination_model');
$this->params['main'] = 'frontend/pages/offer_details';
$this->params['title'] = $data->long_title;
$this->params['breadcumb'] = $this->slugs_model->getSlugName($this->uri->segment(1));
$this->params['data'] = $data;
$this->params['images'] = $this->slugs_model->getOfferImages($data->id);
$this->params['similar'] = $this->slugs_model->getSimilarOffers($data->category, $data->id);
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
$this->params['offers'] = $this->offers_model->getImportantOffers($data->offers, $data->category, $data->id);
You need to generate query results after you get it from model,
e.g: row_array(), this function returns a single result row.
here's the doc: Generating Query Results
try this:
$this->load->model('frontend/destination_model');
$data['destination'] = $this->destination_model->getOffersByDestination($data->id)->row_array();
$this->load->view('view_name', $data);
And in your view echo $destination['attribut_name'];,
or you can print the array, to see if it's work print_r($destination);
How to Update mass records without deleting or replacing old one inserted records. Here mine problem is that last inserted record will replace with latest records.
/*DB::table('syllabuses')
->where('course_id', $curse_id)
->Where('semster',$semesterid)
->delete();*/
$Syllabus = Syllabus::find($id);
foreach ($data as $value)
{
//$Syllabus = new Syllabus;//
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Please try to understand the difference between INSERT and UPDATE first. Definitely update query replace the old values of the relevant record. In your query you are tying updating same "Syllabus".
$Syllabus = Syllabus::find($id);
foreach ($data as $value)
{
//$Syllabus = new Syllabus;//
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
In above code, you find the Syllabus outside of the foreach. Then you update the that found record inside the foreach. If you want to update many "Syllabus", then try this:
foreach ($data as $value)
{
$Syllabus = Syllabus::find("SyllabusID");
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Or else, if you want to remain old records, please try to create new records in db using following code
foreach ($data as $value)
{
$Syllabus = new Syllabus();
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Read official laravel documentation for further details: https://laravel.com/docs/5.7/eloquent
$locationId=Auth::user()->location_id;
$parcelDetails = $this->model
->whereBetween('date',array($fromDate, $toDate))
->where('source_from',$locationId)
->orWhere('destination_to',$locationId)
->with('sourceFrom', 'destinationTo')
->paginate($perPage);
//get();
return $parcelDetails;
i want to get the results where my source_from or destination_to matches $locationId with the dates given.
You can use a nested where to accomplish that:
$locationId=Auth::user()->location_id;
$parcelDetails = $this->model
->whereBetween('date',array($fromDate, $toDate))
->where(function($q){
$q->where('source_from', $locationId);
$q->orWhere('destination_to', $locationId);
})
->with('sourceFrom', 'destinationTo')
->paginate($perPage);
//get();
return $parcelDetails;