How to whereDate, whereMonth a variable? - laravel

I want to display the total payments overall, and total payments for this month using only one variable.
Controller
$payments = Payment::where('user_id', auth()->user()->id)->get();
return view('subscribers.payments', compact(['payments']));
View
<label>Total payments</label>
<p>{{ $payments->sum('amount') }}</p>
<label>For this month</label>
<p>{{ $payments->whereMonth('created_at', now()->month)->sum('amount') }}</p>
Actually the total payments overall displayed. But the "for this month" it is not working and returns me an error below:
Method Illuminate\Database\Eloquent\Collection::whereMonth does not exist.
Someone could tell me where I went wrong? and proper way to achieve this?

Isn't querying by month alone going to create a problem later when you have more than one year of data? I would just use the start of the current month.
<p>{{ $payments->where('created_at', '>=', now()->startOfMonth())->sum('amount') }}</p>

so whereMonth is unfortunately only available with the query builder. You could filter the items after they've been retrieved:
$payments->filter(function ($payment) {
return $payment->created_at->month == now()->month;
})->sum('amount')

Related

How to set default selected option in very big select tag with many options in Laravel?

I have 3 select tags with a lot of options inside them. One for year, one for month and one for day.
The question is ... How to set selected on the option I want.
I was thinking of this solutions:
Pass the options as array to the view. After that forech them in the select tag and with "#if" check every single one if its the right one and set "selected:selected".
But in my case this isnt good solution because I have many options ... that means the array will be very very big.
Put if statement in every option in the html and check if its te right one and set "selected:selected" to that option. This is also a bad solution ... there will be like 200+ if statements.
Maybe create 3 db tables with this options inside. Getting them in the controller method, foreaching them in the select tag and checking them with if statement and setting "selected" to the right one.
I really dont know if Im thinking in the right way. I will apriciate some help.
what you r doing is right here is a solution that it did worked for me, i have a user city, and when i want the user to update the city i show it as selected
just pass the arrays to the view then :
#foreach ($citys as $city)
#if (Auth::user()->city_id == $city->id)
<option value="{{$city->id}}" selected="selected">{{$city->name}}</option>
#else
<option value="{{$city->id}}">{{$city->name}}</option>
#endif
#endforeach
here is a short version u can use
#foreach ($citys as $city)
<option value="{{$city->id}}" #if (Auth::user()->city_id == $city->id) selected="selected" #endif > {{$city->name}}</option>
#endforeach

Display result in graph based on combination of year and month selection in laravel

i am display graph of sum of qty datewise it works but now i want to display graph in which sum of qty of month and year combine selection. My date is stored in format 2020-02-14 and i want to display sum of qty of 2020-02 that is from 2019-02 to 2020-09. I tried lot of works. I am getting graph date wise but now i want to year and month combine
For date selection the query as
$get_details=DB::select('SELECT sum(orders_qty) as sum_of_qty,deliver_date FROM `orders` WHERE deliver_date between ? AND ? GROUP BY deliver_date',[$data['start_date'],$data['end_date']]);
For yearand month selection i need query
I tried like this
$data=$req->all();
$results = DB::table('orders')
->select(DB::raw('DATE_FORMAT(deliver_date,"%y-%m") as deliver_date'),DB::raw('SUM(orders_qty) as sum_of_qty'))
->whereBetween('deliver_date',[$data['start_year_month'],$data['end_year_month']])
->groupBy('deliver_date')
->get();
$date[start_year_month]='2019-02' $date[end_year_month]='2019-05' and actual database date='2019-02-14'
plz need query
First, use %Y-%m instead of %y-%m;
Secondly, you are rewrite your field's name, so group by will not using the name that has been rewritten, you need to specify DB::raw('DATE_FORMAT(deliver_date,"%Y-%m")
So the query like this:
$data=$req->all();
$results = DB::table('orders')
->select(DB::raw('DATE_FORMAT(deliver_date,"%Y-%m") as delivery_date'),DB::raw('SUM(orders_qty) as sum_of_qty'))
->whereBetween(DB::raw('DATE_FORMAT(deliver_date,"%Y-%m")'), [$data['start_year_month'],$data['end_year_month']])
->groupBy(DB::raw('DATE_FORMAT(deliver_date,"%Y-%m")'))
->get();
You can try this!
$results = DB::table('orders')
->select(DB::raw('DATE_FORMAT(deliver_date,"%y-%m") as deliver_date'),DB::raw('SUM(orders_qty) as sum_of_qty'))
->whereBetween('deliver_date',[$data['start_year_month'],$data['end_year_month']])
->groupBy(function ($val) {
return Carbon::parse($val->start_time)->format('y'); });

Calculate hours for training

I have some hesitations about 3 tables which are type_training , training & payment.
In the table type_training, I have a field named price with 4 amounts: for example:
1 hour 00 = 100 euros
1 hour 30 = 150 euros
2 hour 00 = 200 euros
2 hour 30 = 250 euros
In my page Training , I encode 2 recordings for the same student.
The student Dujardin has booked 3 hours for 300 euros.
In my form Payment, is it possible to retrieve the amount of 300 ?
So, in my Model Payment? I must to calculate the difference between the hour start and the hour end?
I don't know how to do ?
Then, after having retrieved the difference of hours in my example we have 3 hours.
How to I sum my 2 recordings in my field Total ? I have tried this?
$typetraining = Typetraining::find($request->fk_typetraining);
$data = $request->all();
$data['total'] = $typetraining->price + $request->????;
Payment::create($data);
In summary:
1) How to retrieve the difference between hour start & hour end,
2) How to calculate the amounts via the duration of my training?
For information, here is my architecture.
I thank you for your help and your explanations.
Edit: Watercamyan 19/09/2019
I adapat this?
createFromFormat('H:i', $request->get('hour_start'))
Per:
<div class="form-group{{ $errors->has('hour_start') ? 'has-error' : '' }}">
<label for="form-group-input-1">Hour start</label>
<input type="text" name="hour_start" id="hour_start" class="form-control" required="required" value="{{ old('hour_start')}}"/>
{!! $errors->first('hour_start', '<span class="help-block">:message</span>') !!}
</div>
Then, in my model Training I have like error message: Undefined variable: typeseances
$start = Carbon::parse($request->get('hour_start'));
$end= Carbon::parse($request->get('hour_end'));
$mins = $end->diffInMinutes($start, true);
$hoursTraining = $mins/60;
$total = $**typeTraining**->price * $hoursTraining;
I have like error message: Undefined variable: typeseances
I think, as JoeGalind said, you should seriously consider re-archetecting this to be simpler. Having to call a TypeTraining object that has nothing but a price, should indeed be moved up to the Training object. However, let me go through a way to solve it with your existing code.
First, as you said, you need to get the number of hours of the requested training. Unfortunately, you need part hours instead of whole hours to change the price. If you needed whole hours, this would be easy, you could use the Carbon method diffInHours(). But we can do it with diffInMinutes(), and then calculate out the partial hour.
First, we need to parse the hours coming in from the form into a Carbon object:
$start = Carbon::parse($request->get('hour_start'));
$end= Carbon::parse($request->get('hour_end'));
Note, I don't know how it is coming in from your form. You might need to parse it differently if the above doesn't work. Something like:
createFromFormat('H:i', $request->get('hour_start'))
or
createFromFormat('H:i:s', $request->get('hour_start'))
Now that you've got a carbon object, we need to calculate out the difference, including the part hours. Again, we'll use the minutes and calculate for part hours:
$mins = $end->diffInMinutes($start, true);
$hoursTraining = $mins/60;
This will yield your multiplier (the number of hours training), something like 2.0 or 2.5 or 2.25, etc. From here, if you have a base price for one hour (which is what I expect is in that TypeTraining model's price field), it is easy:
$total = $typeTraining->price * $hoursTraining;
The hard part, based on the way you have your code set up, is that you must pull the TypeTraining along with the Training, in order to know the price (again - just stick the price on the training to make life easier).
To get the price, something like this:
$training = Training::with('typeTraining')->where('fk_student', $request->get('fk_student)->first();
$price = $training->typeTraining->price;
Now you have the price to plug into the formula above.
This is surely not exact. And pulling the training with the FK on student is probably not what you want. If it is generic training, or there is some other identifier, use that to pull the training to get the price. But you can decide that later. I can only guess at some of this, as I don't know what's coming in, or what your query needs to be, or your relationships, but this should give you an idea. Most importantly, you were asking for how to calculate total, which is answered farther above.
I would recommend the following:
Add a "price" field to the training table. This way, if in the future, you increment that price, all your history stays with the current price.
After saving your "training", go ahead and calculate the hours between both dates using Carbon library, and select your current price from the TypeTraining table using this value.
Store the value on the Training table and then you can easily calculate the sum from anywhere.

Testimonial star rating in laravel

I have stars column in my testimonials table where is gets numbers between 1 to 5 now i want to show stars depend on the number that saves in that column. For example if number is 2 show 2 stars and if is 5 show 5 stars like that.
I know it can be done trough my model but not sure how to do it.
PS: I want to use font-awesome fa fa-star to show stars in my blade.
any idea?
UPDATE
Maybe I need to explain little more, here is my table screenshot As you see there is only 1 column named stars and it gets numbers between 1-5.
Try this in your view:
#for ($i = 0; $i < $star; $i++)
<i class="fa fa-star"></i>
#endfor
More on Laravel blade loops.

Sorting collection in Laravel 4

I have dates in a calendar and am accessing them with the below, they have a category relationship with a Category table and a relationship with a CalEvent table.
$cal_dates = CalDate::whereBetween('date', array($from, $to))
->orderBy('date')
->orderBY('start')
->get();
This returns a list sorted by start date within a range. All good so far and a returned row looks like the below, I can access Category and CalEvent fine i.e. $cal_date->CalEvent->description.
{
"id":"406",
"cal_event_id":"77",
"start":"00:00:00",
"finish":"00:00:00",
"date":"2014-04-06",
"created_by":"0",
"updated_by":"0",
"created_at":"2013-09-21 04:43:25",
"updated_at":"2013-09-21 04:43:25"
}
What I need to is an object like below for my view with the relationships to Category and Cal_Dates table intact:
cal_dates
'12th Feb 2014'
'Array of dates related to category 1 for 12th Feb'
'Array of all other dates not related to category 1 for 12th Feb'
'13th Feb 2014'
'Array of dates related to category 1 for 13th Feb'
'Array of all other dates not related to category 1 for 13th Feb'
...
Any advice much appreciated...
Try this:
$cal_dates = $cal_dates->sortBy(function($cal_date)
{
return $cal_date->date;
});
or modify your query:
$cal_dates = CalDate::with('category')
->whereBetween('date', array($from, $to))
->orderBy('date')
->orderBY('start')
->get();
Then in your view you can access the relationship like the following:
#foreach($cal_dates as $cal_date)
<li> {{ $cal_date->date }} </li>
<!-- category data -->
<li> {{ $cal_date->category->something }}
#endforeach

Resources