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.
Related
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.
In my case it's all about pricing table.
Usually specific column in pricing table is 3
So by default is I can create more than 3 pricing but it will break the view because the view is designed for 3 pricing column in a row.
Usually I do this in view
<Div class="row">
#foreach($products as $product)
<Div class="col">
Pricings detail goes here.
</Div>
#endforeach
</Div>
<Div class="clear break"></div>
So If pricing is 3 the view will be ok, but it will break if pricing more than 3.
How I can deal with this situation?
My logic so far is create new row and clear break class if pricings divided by 3 is decimal like this simulation.
Pricings 3 div by 3 = 1 row n 1 clear break
Pricings 4 div by 3 = 1.xx so 2 row n 2 clear break
Pricings 6 div by 3 = 2 so 2 row n 2 clear break
Pricings 8 div by 3 = 2.xx so 3 row n 3 clear break
With above logic it should not break the view.
Thanks in advance.
If I understand you correctly, you only want a maximum of 3 columns in each row, and if there are any products left you want to add them to a new row.
You would be able to do it with something like this:
<div class="row">
#foreach($products as $product)
#if($loop->index % 3 == 0)
<div class="clear break"></div>
<div class="row">
#endif
<div class="col"></div>
#endforeach
</div>
Using the loop variable (see the documentation) you can check how many times you looped. This way you can start a new row before a 4th column would be added.
I encountered a problem with pagination in my Laravel app.
Normally, when I want to use Laravel pagination with let's say 3 rows for one page, I use Eloquent method paginate/simplePaginate in combination with method latest() like so:
//MyController.php
$posts= Post->latest()->paginate(3);
//MyView.blade.php
#foreach ($posts as $post)
{{ $post->id }}
#endforeach
{{ $posts->links() }}
So when i have 6 posts in my database and I use Laravel pagination + Infinite scroll, that becomes:
6
5 (1. page)
4
--- ('page separator')
3
2 (2. page)
1
But, if user inserts new row into database table before I reach page 2,
the collection shifts and second page of data becomes:
6
5 (1. page)
4
--- POST WITH ID 4 IS DUPLICATED BECAUSE OF SHIFT IN COLLECTION
4
3 (2. page)
2
So, for example --- if user inserts three new rows into database before I reach paginator, then the second page will output the same three table rows as my current first page, but i'd like to output the remaining table rows:
6
5 (1. page)
4
--- AT SOME TIME BEFORE PAGINATION WAS TRIGGERED, POST WITH ID '7' HAS BEEN ADDED
3
2 (2. page) - continues normally with remaining rows (pagination ignores newly added row)
1
Is there any workaround for this problem?
Thanks.
I solved this issue by asking for the first record id of the first page served
// {initial query} $records->orderBy('created_at', 'desc');
if(is_int($last_id)){
$records = $records->where('id', '<=' , $last_id);
}
$records = $records->paginate($per_page);
This way, the paginator get's records starting from that id, ignoring new records
You could try this:
$posts= Post->orderBy('id', 'desc')
->distinct()
->paginate(3);
I'm trying to build a custom grid in Magento, where I have a row for every product ordered including qty. For example, if a customer ordered 3 of a specific product - I need to have 3 rows that all have matching details. Is this possible?
This is for a custom product where each row represents a manufacturing run. I'm not necessarily looking for code examples, but I'm not sure if I can do this with the sql style collection process? Or if I need modify the query and loop through once I've colelcted all the data from the database?
Thanks
Instead of showing the product order item grid, I just added an export to xml button. Then, in the grid just modified the "_exportIterateCollection" method as the following:
foreach ($collection as $item) {
for($i = 0; $i < $item->getQtyOrdered(); $i++ ){
call_user_func_array(array($this, $callback), array_merge(array($item), $args));
}
}
Hope this helps future questions!
I have a category titled 'top 100 products' and , for now, the collection works fine. But what I want to do is dynamically deteremine what # each product is, in relation to the 100 products being listed. In other words, if product one is listed, I can grab the value so I can add "#1" next to the product title.
When product #74 is listed, it will dynamically say "#74". I can calculate the total number of listings using the getSize function but I don't know how to work with that varible to spit out the proper 'sequence' number of each listed product. The reason I use getSize is so the count can be preserved through paging.
To understand the code, please reference the /catalog/product/list.phtml template. In list or grid mode, you will see the following:
Edit. I can't post this code here. It's maddening. Just need to know how to get a while loop to surround the 'foreach' product contorller loop so I can use while (x) < variable_with_total_products_counted and let x++ reside within the product controller for loop. Every time I try to do this, I get syntax errors. As if it won't let the product controller loop (the for each loop) reside inside my while loop which iterates from 0 --> 99 (top 100 products).
How about something like this:
$count = $collection->getSize();
$counter = 1;
foreach($collection as $item) {
// Process item
echo '<p>' . $item->getName . ' (' . $counter . ')</p>';
$counter++;
}
To ensure that number is persistent, you can assign it to the products and it will stay assigned for the duration of the script / page:
$count = $collection->getSize();
$counter = 1;
foreach($collection as $item) {
$item->setData('tmp_number', $counter);
$counter++;
}
// Work with products