I need to get multiple results from different queries on one table.
For example I need to get Count, Sum, Average of one table. Should I do like this or is there a shorter way?
public function index()
{
$count = Patient::all()->count();
$dateCount = Patient::where('date', date("Y-m-d"))->count();
$loanAmount = DB::table('patients')->sum('loan_amount');
$payAmount = DB::table('patients')->sum('pay_amount');
return view('index', compact('count','dateCount','loanAmount' ,'payAmount'));
}
If you see All queries are for one table to get specific results, So basically is there a short way to get these results not by single queries for each?
You can do this by DB query as below :
$data=\DB::table('patients')
->selectRaw('count(id) as count,sum(loan_amount) as loanAmount,sum(pay_amount) as payAmount,sum(case when date = "'.date("Y-m-d").'" then 1 else 0 end) AS dateCount')
->first();
You can also do this using eloquent but it will return you collection.
$data=Patient::selectRaw('count(id) as count,sum(loan_amount) as loanAmount,sum(pay_amount) as payAmount,sum(case when date = "'.date("Y-m-d").'" then 1 else 0 end) AS dateCount')
->first();
You can do something like this.
public function index()
{
$patient = Patient::all();
$count = $patient->count();
$dateCount = Patient::today()->count();
$loanAmount = $patient->sum('loan_amount');
$payAmount = $patient->sum('pay_amount');
return view('index', compact('count','dateCount','loanAmount' ,'payAmount'));
}
Also you can create scope in your patient model:
public function scopeToday($query) {
return $query->where('date', date("Y-m-d"));
}
Related
I have a model Topic which has hasMany relationship with Game model.
//Topic.php
public function Games() {
return $this->hasMany(Game::class);
}
//Game.php
public function Topic() {
return $this->belongsTo(Topic::class);
}
What I need is getting the Topic which has the max count (also the count value) of Games based on 'created_at' today,this week,this month etc. I tried the following code, but the nested whereBetween query does not work, instead it is showing all the related topic->games created ever
$top_topic_month = Topic::with('games')->get()->sortBy(function($q)
{
return $q->games->whereBetween('created_at',[today()->startOfMonth, today()])->count();
})->first();
$top_topic_month_count = ?
try this
use withCount to generate games_count
$top_topic_month = Topic::withCount('games')->whereHas('games',function($q) use($startMonth){
$q->whereBetween('created_at', [$startMonth, today()]);
})->orderByDesc('games_count')->first();
$top_topic_month_count = $top_topic_month->count()
I'm having trouble merging two query results into an array - the merged output contains all the records of one of the queries ($factsheets) but only the last record of the other ($actives) where there are usually at least 3 records "available" to return.
My controller's code is as follows:
public function show($pest)
{
$theactives = self::getActives($pest);
$thefactsheets = self::getFactsheets($pest);
$merged = $theactives->merge($thefactsheets);
$result = $merged->all();
return $result;
}
public function getActives($pest){
$actives = Active::where('pests.id',$pest)
->join("active_pest","actives.id","=","active_pest.active_id")
->join("pests","pests.id","=","active_pest.pest_id")
->select('ai', 'groupcode', 'risk', 'pest')
->orderBy('ai')
->get();
return $actives;
}
public function getFactsheets($pest){
$factsheets = Factsheet::where('pest_id',$pest)
->join("factsheet_pest","factsheets.id","=","factsheet_pest.factsheet_id")
->select('title', 'factsheets.id')
->orderBy('title')
->get();
return $factsheets;
}
Again, my expectation has exceeded my ability - what am I doing wrong here?
you can't merge objects of the result set. So, you have to convert your result into array first before the merge. Try the below script.
public function show($pest)
{
$theactives = self::getActives($pest);
$thefactsheets = self::getFactsheets($pest);
return array_merge($theactives, $thefactsheets);
}
public function getActives($pest){
return Active::where('pests.id',$pest)
->join("active_pest","actives.id","=","active_pest.active_id")
->join("pests","pests.id","=","active_pest.pest_id")
->select('ai', 'groupcode', 'risk', 'pest')
->orderBy('ai')
->get()->toArray();
}
public function getFactsheets($pest){
return Factsheet::where('pest_id',$pest)
->join("factsheet_pest","factsheets.id","=","factsheet_pest.factsheet_id")
->select('title', 'factsheets.id')
->orderBy('title')
->get()->toArray();
}
You can use union in laravel
example
$silver = DB::table("product_silver")
->select("product_silver.name"
,"product_silver.price"
,"product_silver.quantity");
$gold = DB::table("product_gold")
->select("product_gold.name"
,"product_gold.price"
,"product_gold.quantity")
->union($silver)
->get();
then
dd($gold);
I have a query where can i get all the data from related tables and get total 10 rows.Now i want to set pagination parameter in my query. if my pagenumber=1 and limit=2 it returns 1 and 2 rows data with page=1 and limit=2, If I am sending page-3 and limit-2 it returns 5 and 6 rows and page-3.if page=null and limit=null return all data.
How can I do this.
My Function :
Post::with(['product.categories.attributes'])->whereStatus("Active")->get();
Also how can i pass this in POSTMAN
All you need is skip / take
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset
Your code would be something like:
public function show(Request $request) {
$perPage = $request->perpage;
if ($request->page == "") {
$skip = 0;
else {
$skip = $perPage * $request->page;
}
$result = Post::with(['product.categories.attributes'])
->skip($skip)
->take($perPage)
->where("Status", "Active")
->get();
}
Case when $_POST['perpage'] is not set can be explored here:
Skip and take all?
I'm trying to count all the files within a category, and I have these two relationships:
public function files() {
return $this->hasMany('App\File', 'category_id','category_id');
}
public function fileCount() {
return $this->files()->selectRaw("category_id, count(*) AS count")
->groupBy('category_id');
}
This gives me a collection of items, where the count attribute could be accessed like this:
$my_category = Category::where("category_id", category_id)->with('fileCount')->get();
$file_counter = $my_category->first()->fileCount->first()->count;
Is it possible to directly attach the count attribute to the $my_category variable? Any suggestions will be appreciated.
You can use withCount() method:
$my_category = Category::where("category_id", category_id)
->withCount(['files' => function($q) {
$q->groupBy('category_id');
})
->get();
This will put files_count attribute into results.
If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models
I have a Part entity that has a many-to-many relationship with Order and Project. The pivot tables contains a quantity field which might have a positive or negative value, and by adding the sums of both pivot tables, for a specific part, I get the current stock.
I am able to do this, but get the N+1 problem. And I am having a hard time figuring out how I can do it with eager loading. I've read this post, but I don't understand how to adapt it to my needs.
So I am looking for a way to provide a eager loadable stock property on the Part model, suggestions to how I can accomplish this?
I was able to figure it out with the help of this post. This is what I did:
Part model
Create two relationship orderPartsCount and projectPartsCount, add attribute calculatedStock to sum them and provide an easy way of retrieving.
public function orderPartsCount()
{
$a = $this->orders();
$a1 = $a->selectRaw($a->getForeignKey() . ', sum(count) as stock')
->where('done', '>', 0)
->groupBy($a->getForeignKey()
);
return $a1;
}
public function projectPartsCount()
{
$b = $this->projects();
$b1 = $b->selectRaw($b->getForeignKey() . ', sum(count) as stock')
->where('status', '>', 0)
->groupBy($b->getForeignKey()
);
return $b1;
}
public function getCalculatedStockAttribute()
{
$orders = $this->orderPartsCount->first() ? $this->orderPartsCount->first()->stock : 0;
$projects = $this->projectPartsCount->first() ? $this->projectPartsCount->first()->stock : 0;
// invert project parts, since they are listed as positive counts but shall reduce the stock
return $orders + ( $projects * -1);
}
Part controller
Eager load orderPartsCount and projectPartsCount in the controller.
public function index()
{
return View::make('parts.index', [
'parts' => Part::with('category', 'location', 'orderPartsCount', 'projectPartsCount')
->orderBy('description_no')
->paginate(100)
]);
}