Laravel: checking two columns if one matches display the result - laravel

$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;

Related

I want to retrieve the id from the query result and reuse it for the next query laravel

I want to use the id from the select query results, then I want to use that id again to find data using wherein. but the error i found,
public function getMaut(){
$merek="samsung";
$tipe="led TV";
$display ="25-32";
$kriteria=Kriteria::all();
$alternatif = Alternatif::where('merek',$merek)->where('tipe',$tipe)->where('display',$display)->get();
$alternatif_id = (array)$alternatif->id;
$nilaialter = Nilaialternatif::whereIn('id_alternatifs',$alternatif_id);
return view('spk.index',compact('kriteria','alternatif','nilaialter','alternatif_id'));
}
Use the pluck method to retrieve all values for a given key.
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$alternatif_id = $alternatif->pluck('id');
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_id)->get();
Alternatively, you can create a hasMany relation to 'Nilaialternatif' at 'Alternatif' model.
class Alternatif extends Model
{
public function nilaialternatifs()
{
return $this->hasMany(Nilaialternatif::class, 'id_alternatifs');
}
}
Then query the relationship like
$alternatif = Alternatif::with('nilaialternatifs')->where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get();
$nilaialter = $alternatif->nilaialternatifs;
$nilaialter = [];
$alternatif = Alternatif::where('merek', $merek)->where('tipe', $tipe)->where('display', $display)->get()->toArray();
if (count($alternatif)) {
$alternatif_ids = collect($alternatif)->pluck('id')->toArray();
$nilaialter = Nilaialternatif::whereIn('id_alternatifs', $alternatif_ids)->get()->toArray();
}
use laravel collection, https://laravel.com/docs/6.x/collections
use pluck method of collection,https://laravel.com/docs/6.x/collections#method-pluck

Laravel sortByDesc chain with multiple chain

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..

laravel get eloquent return null for specific column if another column is not specific word

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;
}
}

How to call information from one model to another Codeigniter

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);

Multiple Eloquent Where statements with multiple parameters

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();

Resources