Laravel - how to group a Date string on table using collection - laravel

i want to group my date Month but i don't know how to do that. i try used groupBy() but its giving me a error
$user = Auth::user()->id;
$date = productSold::where('user_id',$user)->get();
$collect = collect($date)->map(function ($date) {
return date('M',strtotime($date->created_at))->groupBy('created_at');
});
return $collect;
error :
Call to a member function groupBy() on string
without groupby this is the output :
[
"Oct",
"Oct",
"Nov",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec",
"Dec"
]
i want to group "Oct", "Nov", "Dec"

Its error because you group by it from a string. So you should take out groupBy statement from map function. So the result should be like :
$collect = collect($date)->map(function ($date) {
return [
"created_at" => date('M',strtotime($date->created_at))
];
})->groupBy('created_at');
The result should be like this.

Related

How to return consistent data types in laravel pagination

I have a function that returns staff and there associated attributes as below
foreach ($merchant_user_ac->staffs->sortByDesc('id') as $staff) {
$wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);
$payments = StaffPayment::where('merchant_id', AH::cMiD())
->where('user_id', $staff->id)
->when(!empty($start_date) && !empty($end_date), function ($q) use (
$start_date,
$end_date
) {
$q->whereBetween(\DB::raw('date(payment_date)'), [
$start_date,
$end_date,
]);
})
->sum('amount');
$balance_owed = $wage - $payments;
$transactions->add([
'id' => $staff->id,
'name' => $staff->name,
'profilephoto' => $staff->profilephoto,
'wage' => $wage,
'payments' => $payments,
'salary' => $staff->salary,
'rent' => $staff->rent,
'balance_owed' => $balance_owed + $staff->salary - $staff->rent,
]);
}
$merchant_staffs = collect(json_decode(json_encode($transactions), false));
$merchant_staffs = $merchant_staffs->paginate(10);
return response()->json($merchant_staffs);
In the results, the first page is OK but the subsequent pages are having a different data type from the first page and displaying the data becomes an issue.
The data key has different data types.
I have tried paginating before the foreach loop but the response did not have the pagination links.
I have tried adding ->toArray() method when collecting the data but has the same issue of different types.
How can I return the same data in all pagination links?
The data returned is as below
{
"current_page": 1,
"data": [
{
"id": 532,
"name": "George2",
"profilephoto": "photos/GwSIKoIXUk1GdL7boD7Ht9mSp1loxM1nGcZ5l5Gd.jpg",
"wage": 0,
"payments": 10000,
"salary": "90000.00",
"rent": "1000.00",
"balance_owed": 79000
},
{
"id": 528,
"name": "david",
"profilephoto": null,
"wage": 100,
"payments": 0,
"salary": "67000.00",
"rent": "67000.00",
"balance_owed": 100
},
{
"id": 524,
"name": "Naggie",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 503,
"name": "Khaki ",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 502,
"name": "Susan",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 476,
"name": "Maggie",
"profilephoto": null,
"wage": 0,
"payments": 17000,
"salary": null,
"rent": "15000.00",
"balance_owed": -32000
},
{
"id": 475,
"name": "Aggy",
"profilephoto": null,
"wage": 0,
"payments": 15000,
"salary": "15000.00",
"rent": null,
"balance_owed": 0
},
{
"id": 465,
"name": "Rhoda",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 464,
"name": "Very New Staff",
"profilephoto": null,
"wage": 500,
"payments": 0,
"salary": "10000.00",
"rent": null,
"balance_owed": 10500
},
{
"id": 422,
"name": "jane",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": "15000.00",
"rent": null,
"balance_owed": 15000
}
],
"first_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
"from": 1,
"last_page": 3,
"last_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
"label": "1",
"active": true
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"label": "2",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
"label": "3",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"path": "http://127.0.0.1:8000/api/v1/staff/list",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 25
}
Try
$pagination = $merchant_user_ac->staffs()->latest()->paginate(10);
$data = $pagination->getCollection()->map(function ($staff) {
$wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);
$payments = StaffPayment::where('merchant_id', AH::cMiD())
->where('user_id', $staff->id)
->when(!empty($start_date) && !empty($end_date), function ($q) use (
$start_date,
$end_date
) {
$q->whereBetween(\DB::raw('date(payment_date)'), [
$start_date,
$end_date,
]);
})
->sum('amount');
$balance_owed = $wage - $payments;
return [
'id' => $staff->id,
'name' => $staff->name,
'profilephoto' => $staff->profilephoto,
'wage' => $wage,
'payments' => $payments,
'salary' => $staff->salary,
'rent' => $staff->rent,
'balance_owed' => $balance_owed + $staff->salary - $staff->rent,
];
});
return response()->json($pagination->setCollection($data));
}

How to set X-axis and Y-axis title in a chart in ionic framework?

This is the code that i have used
for inserting the chart
and this is the controller that i have used
.controller('ExampleCtrl', function ($scope) {
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
})
someone please do help me with this issue..!!!

How to change the color of the button when it is clicked in dhtmlx

can anybody help in adding an scolor i would be happy if u help me in doing
menu.setIconsPath("../common/imgs/");
menu.addNewSibling(null, "jan", "JAN"); //these jan to dec are button in menu bar
menu.addNewSibling("jan", "feb", "FEB"); //when it is clicked it must show in another
menu.addNewSibling("feb", "mar", "MAR"); //color
menu.addNewSibling("mar", "apr", "APR");
menu.addNewSibling("apr", "may", "MAY");
menu.addNewSibling("may", "jun", "JUN");
menu.addNewSibling("jun", "jul", "JUL");
menu.addNewSibling("jul", "aug", "AUG");
menu.addNewSibling("aug", "sep", "SEP");
menu.addNewSibling("sep", "oct", "OCT");
menu.addNewSibling("oct", "nov", "NOV");
menu.addNewSibling("nov", "dec", "DEC", false);
Try the next (not public) approach:
menu.attachEvent("onClick", function(id, zoneId, casState){
menu.idPull[menu.idPrefix+id].style.backgroundColor = '#FFC000';
});

In asp.net mvc3 Chart helper not showing the master page

I am trying to use Chart Helper in ASP.NET MVC3. But problem i am facing is,only the concerned chart is displayed without the master page.
My code is :
Controller
public ActionResult Chart()
{
Chart chart = new Chart(width: 600, height: 400)
.AddTitle("Chart")
.AddSeries(
chartType: "line",
legend: "Rainfall",
xValue: new[] { "Jan", "Feb", "Mar", "Apr", "May" },
yValues: new[] { "20", "20", "40", "10", "10" }).AddSeries(chartType: "line", yValues: new[] { "30", "40", "50", "60", "70" }).Write("png");
return null;
}
View
#model dynamic
#{
ViewBag.Title = "Chart";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Chart</h2>
<h2>About</h2>
<p><img src="#Url.Action("Chart")" alt="hello chart" /></p>
Please help me to find where i am going wrong.
Thanks in Advance
You need to return the chart as an image. Try this:
public ActionResult Chart()
{
Chart chart = new Chart(width: 600, height: 400)
.AddTitle("Chart")
.AddSeries(
chartType: "line",
legend: "Rainfall",
xValue: new[] { "Jan", "Feb", "Mar", "Apr", "May" },
yValues: new[] { "20", "20", "40", "10", "10" }).AddSeries(chartType: "line", yValues: new[] { "30", "40", "50", "60", "70" })
.GetBytes("png");
return File(chart, "image/png");
}

Flot not displaying x axis labels correctly

I have to display a graph with date on the X axis and Amt on the Y axis. There will be 8 lines (series) each with n months data.
When I plot the graph I am sending in 6 months data for sure.( one line's data is shown below)
[1251701950000, 34.50553]
[1254294030000, 27.014463]
[1256972350000, 26.7805]
[1259567970000, 33.08871]
[1262246430000, 51.987762]
[1264924750000, 56.868233]
However the graph shows up like this http://twitpic.com/1gbb7m
The first months label is missing and last month is not aligned correctly, my flot js code is as follows
$.plot($("#lgdGraphTab"),graphData, {
xaxis: {
mode: "time",
timeformat: "%b-%y",
monthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
minTickSize: [1, "month"]
},
yaxis : {
tickSize: 5
},
series: {
lines: { show: true , shadowSize:0},
points: { show: true }
},
legend:{
container: $('#legendArea'),
noColumns:8
},
clickable: true,
hoverable: true
});
All of the timestamps in your data hold the last day of each month rather than the 1st. I believe this is the cause of your problem.

Resources