Print the name of function has highest value - php

My 3 different function given me value as below: in JSON.
I want to PRINT name of function which has highest value. The Functions are;
public function firstHourTrades()
{
$user_id = Auth::user()->id;
$data = DB::table('finaltrade')
->select(DB::raw('count(*) as first_hour'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
->get();
return response()->json($data);
}
public function lastHourTrades()
{
$user_id = Auth::user()->id;
$data = DB::table('finaltrade')
->select(DB::raw('count(*) as last_hour'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw('exchanges.close_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw("SUBTIME(exchanges.close_time, '01:00:00')"))
->get();
return response()->json($data);
}
public function otherHoursTrades()
{
$user_id = Auth::user()->id;
$data = DB::table('finaltrade')
->select(DB::raw('count(*) as other_hours'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereRaw('finaltrade.created_at NOT BETWEEN exchanges.start_time AND DATE_ADD(exchanges.start_time, INTERVAL 1 HOUR)')
->whereRaw('finaltrade.created_at NOT BETWEEN exchanges.close_time AND DATE_SUB(exchanges.close_time, INTERVAL 1 HOUR)')
->get();
return response()->json($data);
}
FROM THE ANSWER I HAVE CREATED:
public function allCmp()
{
$func_names = ['firstHourTrades()','lastHourTrades()','otherHoursTrades()'];
foreach($func_names as $name)
$results[$name] = $name();
$max = max($results);
DD($max);
}

<?php
function one()
{
return 1;
}
function two()
{
return 2;
}
function three()
{
return 3;
}
$func_names = ['three','one','two'];
// Call each function and store result.
foreach($func_names as $name)
$results[$name] = $name();
$max = max($results);
$max_funcs = array_keys($results, $max);
var_dump($max_funcs);
Output:
array(1) {
[0]=>
string(5) "three"
}
You may need to decode your JSON at some point, something like:
function get_val_from_my_json($json) {
return array_values(json_decode($json, true))[0];
}
print get_val_from_my_json('{"some_key":5}');
Output:
5
And run all the results through that function (assuming valid json).
$results = array_map('get_val_from_my_json', $results);

Related

How to call all variable from other method and return it in same view

I need help. I want to pass all variabel, call in index, and return in same view. How to call perFolder method and call the variabel in perFolder method such as userj.
class fileCount extends Controller {
public function index() {
$fj = $this->perFolder();
$users = DB::table('files')->where('created_by_id', '=', '1')->count();
$users2 = DB::table('files')->where('created_by_id', '=', '2')->count();
$users3 = DB::table('files')->where('created_by_id', '=', '3')->count();
$users4 = DB::table('files')->where('created_by_id', '=', '4')->count();
$users5 = DB::table('files')->where('created_by_id', '=', '5')->count();
$users6 = DB::table('files')->where('created_by_id', '=', '6')->count();
$users7 = DB::table('files')->where('created_by_id', '=', '7')->count();
$users8 = DB::table('files')->where('created_by_id', '=', '8')->count();
$users9 = DB::table('files')->where('created_by_id', '=', '9')->count();
$users10 = DB::table('files')->where('created_by_id', '=', '10')->count();
$users71 = DB::table('files')->where('created_by_id', '=', '11')->count();
//$row_count=DB::table('media')->where('file_name', '>', '1')->count();
$row_count = DB::table('media')
->select('file_name', DB::raw('COUNT(*) as `count`'))
->groupBy('file_name')
->havingRaw('COUNT(*) > 1')
->get();
//$row_count = DB::table('media')->where('file_name', '=', $file_name)->count();
return view('admin.report.index2', compact('users', 'users2', 'users3', 'users4', 'users5', 'users6', 'users7', 'users8', 'users9', 'users10', 'users71', 'row_count'));
}
public function perFolder() {
$userj = DB::table('files')->where('created_by_id', '=', '1')->where('folder_id', '=', '1')->count();
$userj2 = DB::table('files')->where('created_by_id', '=', '2')->where('folder_id', '=', '2')->count();
return view('admin.report.index2', compact('userj','userj2'));
}
}

How can I display other data in foreach?

In my show function in which you can watch every single group, I load all articles with the same tags. And the article with the most votes will be displayed separately at the top. In addition, I indicate the average rating of the items in this group. This is how my function looks like:
public function show($id)
{
$group = group::find($id);
$tagIdArray = $group->tags->pluck('id')->all();
$mostvotedarticle = Article::where('type', 4)->whereIn('privacy', [1, 3])
->whereHas('tags', function($query) use ($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))
->orderByVotes()->first();
$articledown = Article::where('status', 1)->whereHas('tags', function($query) use ($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))->downVotesAll()->count();
$articleup = Article::where('status', 1)->whereHas('tags', function($query) use ($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))->upVotesAll()->count();
$ratio = null;
if($articleup + $articledown == 0) {
$ratio = 0;
} else {
$ratio = ($articleup*100)/($articleup + $articledown);
}
return view('singlegroup',compact('groups','mostvotedarticle', 'ratio'));
}
On a overview page, the individual groups are displayed with a foreach loop:
function:
public function index()
{
$user = Auth::user();
$groups = $user->groups()->latest()->with('tags')->paginate(20);
return view('groups', compact('groups'));
}
view:
#foreach ($groups as $group)
{{$group->name}}
<img src="-----load mostvotedarticle that is in this group------" alt="" />
<div class="progress-bar-primary" style="width:{{$ratio}}%;">
#endforeach
How can I have the mostvotedarticle and group ratings displayed in the foreach loop?
Article Model
public function scopeOrderByVotes($query)
{
$query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
->selectRaw('articles.*, count(Article_activities.id) as aggregate')
->groupBy('articles.id')
->orderBy('aggregate', 'desc');
}
public function scopeDownVotesAll($query)
{
$query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
->selectRaw('articles.*, count(Article_activities.id) as aggregate')
->where('Article_activities.activity', '=', '0');
}
public function scopeUpVotesAll($query)
{
$query->leftJoin('Article_activities', 'Article_activities.article_id', '=', 'articles.id')
->selectRaw('articles.*, count(Article_activities.id) as aggregate')
->where('Article_activities.activity', '=', '1');
}
you need put all inside your foreach groups
public function index()
{
$user = Auth::user();
$groups = $user->groups()->latest()->with('tags')->paginate(20);
$ratio = null;
foreach($groups as $key => $group)
{
$tagIdArray = $group->tags->pluck('id')->all();
$mostvotedarticle = Article::where('type', 4)->whereIn('privacy', [1, 3])
->whereHas('tags', function($query) use ($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))
->orderByVotes()->first();
$articledown = Article::where('status', 1)->whereHas('tags', function($query) use
($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))->downVotesAll()->count();
$articleup = Article::where('status', 1)->whereHas('tags', function($query) use
($tagIdArray) {
$query->whereIn('tags.id', $tagIdArray);
}, '>=', count($tagIdArray))->upVotesAll()->count();
if($articleup + $articledown == 0) {
$ratio = 0;
} else {
$ratio = ($articleup*100)/($articleup + $articledown);
}
$group->mostVote = $mostvotedarticle;
$group->ratio = $ratio;
}
return view('groups', compact('groups'));
}

How to merge these two function with Laravel query builder

I need to export final amount after calculation of all payable and receivable of a member now I do in two function how to merge them
public function memberReceivable($id){
return $this->join('transactions', 'transactions.id', '=', 'member_transactions.transaction_id')
->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount")
->where('accounts.type','accountsReceivable')
->where('member_transactions.member_id',$id)
->groupBy('transactions.unit')->get();
}
public function memberPayable($id){
return $this->join('transactions', 'transactions.id', '=', 'member_transactions.transaction_id')
->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount")
->where('accounts.type','accountsPayable')
->where('member_transactions.member_id',$id)
->groupBy('transactions.unit')->get();
}
public function member($id = null, $type = ""){
$response = $this->join('transactions', 'transactions.id', '=',
'member_transactions.transaction_id')
->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount");
if($type){
$response->where('accounts.type', $type);
}
if($id){
$response->where('member_transactions.member_id',$id);
}
$response->groupBy('transactions.unit')->get();
return $response;
}
You can try it.

Loop over Laravel Collection

public function showJobCategoryContent($id){
$jobsInfoById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
// Imagine here i got 5 data
foreach ($jobsInfoById as $jobInfoById) {
return $current=$jobInfoById->created_at;
//$trialExpires []= $current->addDays(30);
}
}
If i loop it it only show 1 data. How is it possible If use array sign then it will show 1 data.
If you want to get an array of all created_at dates, you don't need a loop.
public function showJobCategoryContent($id)
{
$jobsInfoById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
return $jobsInfoById->pluck('created_at');
}
Using the loop:
public function showJobCategoryContent($id)
{
$jobsInfoById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$dates = [];
foreach ($jobsInfoById as $jobInfoById) {
$dates[] = $current = $jobInfoById->created_at;
}
return $dates;
}
If want to add 30 days to each date:
public function showJobCategoryContent($id)
{
$jobsInfoById = DB::table('jobs')->where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$jobsInfoById = $jobsInfoById->map(function ($job) {
return $job->created_at->addDays(30);
});
return $jobsInfoById->pluck('created_at');
}
Have a look at Laravel collections and how they can be useful: link

Laravel Controller - Pass a variable from function to function

So I have two functions named 'search' and 'pdfview'.
Every time I use the search function, I want to pass a query where it selects certain data to the pdfview function.
Here's my search function:
public function search(Request $request)
{
if ($request->period == 'Daily') {
$datefrom = Carbon::now()->startOfDay();
$dateto = Carbon::now()->endOfDay();
}
elseif ($request->period == 'Weekly') {
$datefrom = Carbon::now()->startOfWeek();
$dateto = Carbon::now()->endOfWeek();
}
elseif ($request->period == 'Monthly') {
$datefrom = Carbon::now()->startOfMonth();
$dateto = Carbon::now()->endOfMonth();
}
elseif ($request->period == 'Yearly') {
$datefrom = Carbon::now()->startOfYear();
$dateto = Carbon::now()->endOfYear();
}
else {
$datefrom = Carbon::parse($request->datefrom);
$dateto = Carbon::parse($request->dateto);
}
$solditems = DB::table('orders')
// ->join('orders', 'receipts.receipt_id', '=', 'orders.receipt_id')
->whereDate('orders.created_at', '>=', $datefrom)
->whereDate('orders.created_at', '<=', $dateto)
->where('status','=', 'served')
->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
->groupBy('item_id')
->orderBy('created_at', 'dsc')
->get();
return view('salesreports.sellingitems.index', compact('solditems'));
}
And here's my pdfview function:
public function pdfview(Request $request)
{
$solditems = DB::table('orders')
->where('status', 'served')
->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
->groupBy('item_id')
->orderBy('qty', 'dsc')
->get();
view()->share('solditems', $solditems);
if ($request->has('download')) {
$pdf = PDF::loadView('salesreports.sellingitems.pdf');
return $pdf->download('sellingitems-' . Carbon::now() . '.pdf');
}
return view('salesreports.sellingitems.pdf');
}
As you can see, the pdfview function has a default query for the $solditems. But I wanted that variable to come from my search function.
UPDATE: I managed to solve the problem using session.
Here's how you store with session:
session()->put('period', 'Daily');
and this is how you retrieve with session:
$period = $request->session()->get('period', 'default');
public function getSolditems()
{
if (!isset($this->solditems)) {
$solditems = DB::table('orders')
// ->join('orders', 'receipts.receipt_id', '=', 'orders.receipt_id')
->whereDate('orders.created_at','>=', $datefrom)
->whereDate('orders.created_at', '<=', $dateto)
->where('status','=', 'served')
->select('orders.*', DB::raw('SUM(subtotal) as subtotal'), DB::raw('SUM(qty) as qty'))
->groupBy('item_id')
->orderBy('created_at','dsc')
->get();
$this->solditems = $solditems;
} else {
$solditems = $this->solditems;
}
return $solditems;
}
public function search(Request $request)
{
....
$solditems = getSolditems();
....
}
public function pdfview(Request $request)
{
....
$solditems = getSolditems();
....
}
I think you can use $request->merge() method to pass $solditems data to pdfview() method from search()
public function search(Request $request)
{
....
$request->merge(['solditems'=>$solditems]);
$this->pdfview()
}
public function pdfview(Request $request)
{
$solditems = $request->get('solditems');
....
}

Categories