I want to show the stats of visitors on my website,
What I have:
AdminClontroller
public function index()
{
$select_stats = DB::table('visitor')->whereBetween('visit_date', array(date('Y-m-01'), date('Y-m-t')))->get();
$month = date('m');
$get_count = DB::select(DB::raw("SELECT count(*) FROM visitor WHERE MONTH(date) = $month GROUP BY DAY(date)"));
foreach($select_stats as $statics) {
//change our object to an array
$statics = array();
}
return View::make('admin.home.index')->with('stats', $select_stats)/*->with('count', $get_count)*/;
}
database model
View
var visitors = [
#foreach($stats as $stat)
['{{ date("d/m/Y", strtotime($stat->date)) }}', 55],
#endforeach
];
I want to get a stat like this:
But with the correct data from the database.
I want that I get the count of all IP's that visited my site on day 'x', and that for a month long.
So: on 2015-04-14 I got 20 visits, that is what I want to get, but looped in my template.
I hope you guys do understand me.
Kindest regards,
Related
In my table, I have 2 columns:
1-count_of_h 2- updated_at
Now I want select rows which is past the updated_at, count_of_h hours.
A code like this:
$rows = Model::where('updated_at', '<', Carbon::now()->subHours($row->count_of_h))->get();
But I don't know how can I do it.
My code is kinda dirty ( but it works )
$arrayOfData = [];
Model::chunk(100, function($model) {
foreach($model as $m) {
if($m->updated_at < Carbon::now()->subHours($m->count_of_h)) {
$arrayOfData[] = $m;
}
}
});
First is make an array. After that, it will take every 100 rows then check if its past count_of_h hours
My Code Sum Products and quantity and price but i need 3 functions for day year month in first i will get total of products in every day next in every month and last in every year
the Code
$product_orders = \App\OrderProduct::select(DB::raw('sum(line_total) as total_line'),
DB::raw('sum(qty) as qty') , 'product_id','line_total','created_at')->
orderBy('created_at')->groupBy('product_id')->get();
DB
id , prdouct_id ,order_id, qty ,unit_total , line_total , created_at , updated_at
##for grouping you can use it like these
public function day(){
$products= OrderProduct::select(
DB::raw('DAY(created_at) as day'),DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('day','product_id')->get();
}
public function month(){
$products= OrderProduct::select(
DB::raw('MONTHNAME(created_at) as month'),
DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('month','product_id')->get();
}
public function year(){
$products= OrderProduct::select(
DB::raw('YEAR(created_at) as year'),
DB::raw('sum(line_total) as total_line') , 'product_id','qty','line_total','created_at')->groupBy('year','product_id')->get();
}
try this and let me know what happened
try this:
$Rspatients = DB::table('reports')
->select(
DB::raw("day(created_at) as day"),
DB::raw("Count(*) as total_patients"))
->orderBy("created_at")
->groupBy(DB::raw("day(created_at)"))
->get();
$result_patients[] = ['day','Patients'];
foreach ($Rspatients as $key => $value) {
$result_patients[++$key] = [$value->day,$value->total_patients];
}
then you can pass it to your view like this:
return
view('Dashboard.index')
->with('result_patients',json_encode($result_patients));
I used google charts that is why I sent my data as a json to the view, the data is then feed to the line chart of google chart.
hello i have this function
//Get Total Number of Personal Leads By User ID & Requested week Through Each Day
//Get Total Number of Personal Leads By User ID & Requested week Through Each Day
$personalleads = \DB::table('leads')
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 7) // 7 = Personal Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as pleads'))
->groupBy('date')
->get(); // Get All Data
//Get Total Number of leads Created by Managers By User ID & Requested week Through Each Day
$managerleads = \DB::table('leads')
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 3) // 3 = Manager Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as mleads'))
->groupBy('date')
->get(); // Get All Data
//Get Total Number of leads Created by Admins By User ID & Requested week Through Each Day
$adminleads = \DB::table('leads')
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 4) // 4 = Admin Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as aleads'))
->groupBy('date')
->get(); // Get All Data
i want to return the data of all of them
like
return $adminleads+personalleads+managerleads;
i know this is invalid but i want to display all data at once
this is the output i get when i return $personalleads only
[{"date":"2019-02-10","pleads":1},{"date":"2019-02-12","pleads":1},{"date":"2019-02-14","pleads":1}]
how can i make it something like
[{"date":"2019-02-10","pleads":1,"aleads":1,"mleads":1},{"date":"2019-02-12","pleads":1,"aleads":1,"mleads":1},{"date":"2019-02-14","pleads":1,"aleads":1,"mleads":1}]
Thank you very much
Two options you can try to join these values:
PHP will loop the results and join the data.
The queries handle the joining of data (which I cannot wrap my head around at this moment).
Not sure which one would be better. PHP looks much easier however, depenending on the system and size, my guess is that mysql can handle this much more efficient.
PHP
$pLeads = ...
$mLeads = ...
$aLeads = ...
$allLeads = $pLeads->merge($mLeads)->merge($aLeads); // Maybe there is a shorter variant.
$leads = [];
$types = ['pleads', 'mleads', 'aleads'];
$allLeads->each(function ($lead) {
// Make sure there is an array present with the date property.
data_fill($leads, $lead->date, [
'date' => $lead->date,
]);
// At this point our array looks like:
// ['2019-11-06' => ['date' => '2019-11-06']]
// Get the the type of lead.
$type = $types[array_search(array_keys($lead))];
// Set the lead amount for the type.
// Not sure if "$lead->date.$type" works. Make sure you end up with a key like 2019-11-06.pleads
data_set($leads, "$lead->date.$type", $lead->$type);
// At this point our array looks like:
// ['2019-11-06' => ['date' => '2019-11-06', 'pleads' => 1]]
});
// Remove the keys from the array.
$leads = array_flatten($leads);
// At this point our array looks like:
// [['date' => '2019-11-06', 'pleads' => 1, 'mleads' => 2, 'aleads' => 3]]
Query
SELECT
IFNULL(p.date, m.date)
pleads,
mleads
FROM (
(
SELECT
DATE(created_at) as date,
COUNT(*) pleads
FROM
leads
WHERE
...
GROUP BY
date
) p
RIGHT JOIN
(
SELECT
DATE(created_at) as date,
COUNT(*) mleads
FROM
leads
WHERE
...
GROUP BY
date
) m ON p.date = m.date
);
Both solutions have not been tested fully.
Hello i get stuck on this proble, hope you guys can give me some advice to solve this matter. So, i want to show graph that count basen on month, how many keluhan (complain) on that month. so the table will be like january has 3 complain, febuary has 5 complain, etc (example)
public function lihatkeluhan(){
$halaman="tindaklayanan";
$keluhan_list=DB::table('keluhans')
->select(DB::raw('id,tanggal,produk,username,area,masalah,status'))->get();
$count = count($keluhan_list); //this still not count based on month
$population = Lava::DataTable();
$population->addDateColumn('Year')
->addNumberColumn('Keluhan')
->addRow(['?',$count]);
Lava::LineChart('Population', $population, [
'title' => 'Tahun : 2017',
]);
return view('layanankonsumen.daftarkeluhan',compact('halaman','keluhan_list','lava'));
}
try query like this.
it will gives you groupby result with count of that particular month,
no need for $count = count($keluhan_list); because you will get count in result.
$keluhan_list=DB::table('keluhans')
->select(DB::raw('id,tanggal,produk,username,area,masalah,status,count(*) as count'))
->get()
->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('m'); // grouping by months
});
I have an eloquent query that is returning a 'show', and any 'show_ref's relating to it:
Show::where('show_slug','=',$id)->with('showrefs')->firstOrFail();
This brings back a nested array successfully, as follows:
{
"show_id":2,
"show_name":"ShowNo1",
"show_logo":null,
"show_facebook_url":null,
"show_twitter_url":null,
"show_website_url":null,
"show_slug":"shown1",
"created_at":"2014-09-27 11:57:32",
"updated_at":"2014-09-27 11:57:32",
"showrefs":[
{
"show_ref":1,
"show_id":2,
"show_start_date":"2014-05-16",
"show_end_date":"2014-05-21",
"show_year":2014,
"show_common_name":"Mid May",
"venue_id":null,
"created_at":"2014-09-27 12:17:40",
"updated_at":"2014-09-28 06:26:54"
},
{
"show_ref":2,
"show_id":2,
"show_start_date":"2014-05-23",
"show_end_date":"2014-05-28",
"show_year":2014,
"show_common_name":"Late May",
"venue_id":null,
"created_at":"2014-09-27 12:21:24",
"updated_at":"2014-09-28 06:26:58"
},
{
"show_ref":4,
"show_id":2,
"show_start_date":"2013-02-12",
"show_end_date":"2013-02-13",
"show_year":2013,
"show_common_name":"Early February",
"venue_id":null,
"created_at":"2014-09-28 17:50:02",
"updated_at":"2014-09-28 17:50:02"
},
{
"show_ref":5,
"show_id":2,
"show_start_date":"2013-04-27",
"show_end_date":"2013-04-28",
"show_year":2013,
"show_common_name":"Late April",
"venue_id":null,
"created_at":"2014-09-28 17:50:42",
"updated_at":"2014-09-28 17:50:42"
}
]
}
What I am trying to get from my view is the following format:
ShowNo1 (show name)
2014
list of any shows in 2014
2013
List of any shows in 2013
Is there anyway this is possible from what I have coded so far? or have I gone about this the wrong way?
Thanks.
You have a showrefs function in your Show model somewhere that is defining this relationship. #Armon's answer will work, but will run on the order of O(n * years) where years is the number of years you want to show. Your example only has 2, but could be many more, and you can instead display your data in O(n) time.
You can orderBy in the methods defining relationships.:
// app/models/Show.php
public function showrefs()
{
return $this->hasMany('ShowRef')->orderBy('show_start_date', 'DESC');
}
If the tables you are joining together both contain a column which you want to order by, you may need to qualify the order by by appending the table name, as in:
->orderBy('`show_refs`.`show_start_date`', 'DESC')
After that, its just a matter of keeping track of the current year and noticing when it changes when looping over your data:
$show = Show::where('show_slug','=',$id)->with('showrefs')->firstOrFail();
$currentYear = '';
foreach($show->showrefs as $showref) {
$showRefYear = strtok($showref->show_start_date, '-');
if($currentYear != $showRefYear) {
$currentYear = $showRefYear;
// Output Year heading
}
// Output show details
}
Without knowing anything about Eloquent, you can use a simple function to walk through your nested array and collect your shows for a given year:
function collect_shows_with_year($shows, $year)
{
$result = array();
foreach($shows["showrefs"] as $show_ref)
{
if($show_ref["show_year"] == $year)
{
$result[] = $show_ref;
}
}
return $result;
}
$shows_in_2014 = collect_shows_with_year($shows, 2014);
$shows_in_2013 = collect_shows_with_year($shows, 2013);