Add numbering to the view that contain condition - laravel

I have a problem where I want to add index numbering 1.2.3.4..etc at my table but the table content have #if($t1,t2,t3,..etc > 0) condition on it, if I hard code the number in view, if it meets $t1,t2,t3,..etc < 0, it will skip the index number
<thead>
<tr>
<th>No.</th>
<th>Subject name</th>
<th>No. of students</th>
</tr>
</thead>
<tbody>
#if($t1>0)
<tr class="gradeX">
<td>1</td>
<td><a href="{{url('/admin/technology-and-informationsystem')}}" target="_blank">Technology & Information System</td>
<td style="text-align:center">{{ $t1 }}</td>
</tr>
#endif
#if($t2>0)
<tr class="gradeX">
<td>2</td>
<td><a href="{{url('/admin/discrete-structure')}}" target="_blank">Discrete Structure</td>
<td style="text-align:center">{{ $t2 }}</td>
</tr>
#endif
The output should show the numbering without skipping any number even it skip the condition.

You could move the #if-conditions to only surround the table data that you don't want to be visible unless the condition is met. If I understand your question correctly, you want to show every row with their number and only show the rest of the row if the condition is met. If this is what you want, you could do:
<tr class="gradeX">
<td>1</td>
#if($t1>0)
<td><a href="{{url('/admin/technology-and-informationsystem')}}" target="_blank">Technology & Information System</td>
<td style="text-align:center">{{ $t1 }}</td>
#endif
</tr>
<tr class="gradeX">
<td>2</td>
#if($t2>0)
<td><a href="{{url('/admin/discrete-structure')}}" target="_blank">Discrete Structure</td>
<td style="text-align:center">{{ $t2 }}</td>
#endif
</tr>

Related

Laravel 9 Calculate minute difference same_id

I have a column with id and a column with timestamp and a state column, every time the state changes it writes a time in the timestamp column, I want a calculation of the time difference between the various states, for example if it is less than 15 minutes it is ok, if more than 15 minutes is not ok, how can I do to automate it?
it works perfectly but now i need to specify the same same_id to not count all of the table but only the time referring to the same same_id
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>same_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2023-01-11 15:26:23</td>
<td>NotAvailable</td>
<td>80</td>
<td>2</td>
<td>2023-01-11 15:26:55</td>
<td>ToBeAssigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:06</td>
<td>Assigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:19</td>
<td>TakingCharge</td>
<td>80</td>
<td>4</td>
<td>2023-01-11 15:29:05</td>
<td>Closed</td>
<td>80</td>
</tr>
</tbody>
</table>
If you use Carbon, you should take a look at diffInMinutes method (read more about that here)
Example:
echo (new Carbon\Carbon('2023-01-11 15:26:23'))->diffInMinutes((new Carbon\Carbon('2023-01-11 15:28:55'))); // 2
If you want to do it with plain PHP, please have a look at this answer: https://stackoverflow.com/a/12382882/14714168
Update:
With the data you provided, I think one way to get it is the following:
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>delay</th>
</tr>
</thead>
<tbody>
#foreach((array)$tickets_logs as $i => $ticket_log)
<tr>
<td>{{ $ticket_log['id'] }}</td>
<td>{{ __($ticket_log['states']) }}</td>
<td>{{ $ticket_log['time_stamp'] }}</td>
<td>
#if(!$loop->first)
{{ (new Carbon\Carbon($tickets_logs[$i - 1]['time_stamp']))->diffInMinutes((new Carbon\Carbon($ticket_log['time_stamp']))) }}
#else
-
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
read more about loop variables here

sum in blade how to do it for the subtotal of two tables?

i found out a way by someone asking a question, of a way of producing subtotals, i now just wanted to sum values between two tables , in the code from the person i tested and it works perfectly, i just wanted to aggregate more one table to any of the querys , it's summing on the blade.
//resumed controller
public function index() {
$emphayas = DB::select('select * from emphayas');
$treasuries = DB::select('select * from treasuries');
return view('database',['emphayas'=>$emphayas,
'treasuries'=>$treasuries,
]);}
-----------------------------------------------------------------
//resumed blade
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($emphayas as $emphaya)
<tr>
<td></td>
<td>{{ $emphaya->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') }}
</tr>
</table>
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($treasuries as $treasury)
<tr>
<td></td>
<td>{{ $treasury->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Treasury::sum('available_for_use_month') }}
</tr>
</table>
in this case what i want to do is connect the total of treasury to emphyas
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') + \App\Models\Treasury::sum('available_for_use_month') }}
</tr>

Calculate difference in minutes between created_at and current time in Laravel

I want to show in a field the time someone is waiting, and I want to do that by subtracting created_at time by current time in minutes
this is my table
<thead>
<tr>
<th scope="col">Ordernummer</th>
<th scope="col">E-mail</th>
<th scope="col">Kenteken</th>
<th scope="col">Wachttijd</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
#foreach($orders as $order)
<tr>
<th scope="row">{{ $order->ordernumber }}</th>
<td scope="row">{{ $order->email }}</td>
<th scope="row">{{ $order->numberplate }}</th>
<td scope="row">{{ $order->created_at }}</td>
<td scope="row">
#if( $order->status == 0)
Open
#else
Afgehandeld
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
And where this is written <td scope="row">{{ $order->created_at }}</td>
I want to show there the difference in minutes.
Thanks already
I assume your $order->created_at is Carbon instance, so you can use:
<td scope="row">{{ $order->created_at->diffInMinutes(\Carbon\Carbon::now()) }} minutes ago</td>
if $order->created_at is pure mysql format not Carbon instance just convert/create it:
\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->created_at)->diffInMinutes(\Carbon\Carbon::now())
You can use Carbon to calculate difference in minutes like so
<td scope="row">{{ now()->diffInMinutes($order->created_at) }}</td>
you can use Carbon::diffInMinutes to get the minutes differnt,
or you can use Carbon::diffForHumans to get more readable statement
$order->created_at->diffInMinutes(Carbon::now());
// that would be: 5
$order->created_at->diffInMinutes(Carbon::now());
and this would be: 5 minuets ago

Show data with certain condition only in blade

I have a problem where I don't know how to show the subject where the sum '>0', I managed to show all of the subjects, but it shows with the subject that has sum '0' too. I'm not sure where to put the condition code, is it in view or controller.
this is my controller
public function showSubjectListFinalYear(){
$t1 = DB::table('matrices')->where('total_subject_left','<',10)->sum('teknologi_dan_sistem_maklumat');
$t2 = DB::table('matrices')->where('total_subject_left','<',10)->sum('struktur_diskrit');
$t3 = DB::table('matrices')->where('total_subject_left','<',10)->sum('teknik_pengaturcaraan_1');
$t4 = DB::table('matrices')->where('total_subject_left','<',10)->sum('logik_digital');
return view('admin.final_year_subject_list')>with(compact('t1','t2','t3','t4'));
}
this is the code in my view
<thead>
<tr>
<th>Subject name</th>
<th>No. of students</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>Teknologi dan Sistem Maklumat</td>
<td style="text-align:center">{{ $t1 }}</td>
</tr>
<tr class="gradeX">
<td>Struktur diskrit</td>
<td style="text-align:center">{{ $t2 }}</td>
</tr>
<tr class="gradeX">
<td>Teknik Pengaturcaraan 1</td>
<td style="text-align:center">{{ $t3 }}</td>
</tr>
<tr class="gradeX">
<td>Logik Digital</td>
<td style="text-align:center">{{ $t4 }}</td>
</tr>
try this
$t1 = matrices::where('total_subject_left','>',10)->orderBy('id', 'ASC')->sum('teknologi_dan_sistem_maklumat');
it will show you subject with sum greater than 10. if you replace 10 with 0 it will show subjects with sum greater than 0
assuming you are using
use/app/model name
Solving this issue by adding this code at view.blade file
#if($t1>0)
<tr class="gradeX">
<td>Teknologi dan Sistem Maklumat</td>
<td style="text-align:center">{{ $t1 }}</td>
</tr>
#endif

Laravel+vuejs, how to loop inside table td tag?

Below shows my data. I am trying to loop Item inside table <td> tag so that it forms two items in one column.
user
>"name":"Bob"
>"id":1
>"Item":
>"name":"Desk"
>"name":"Chair"
Code
<table class="table table-hover">
<tbody>
<tr>
<th>Name</th>
<th>Items</th>
</tr>
<tr v-for="use in user.data" :key="use.id">
<td>{{ use.name }}</td>
<td v-for="a in use.item">
{{ a.name }},
</td>
</tr>
</tbody>
</table>
But when I do the codes above, it produce another column just like below picture. How do I put the Desk and Chair sits together in one column?
Hi just use a span tag in side td here is my solution, p or div tag are block tags, if you are using bootstrap 4 just set class to span inline-block or inline
<table class="table table-hover">
<tbody>
<tr>
<th>Name</th>
<th>Items</th>
</tr>
<tr v-for="use in user.data" :key="use.id">
<td>{{ use.name }}</td>
<td>
<span v-for="a in use.item"> {{ a.name }}, </span>
</td>
</tr>
</tbody>
</table>
v-for crates another elemenrs for you in each iteration. Use join function to display together
<tr v-for="use in user.data" :key="use.id">
<td>{{ use.name }}</td>
<td>
{{ use.item.join(", ") }},
</td>
</tr>
Output would be Desk, Chair
We can do this with using lodash
`
<tr v-for="use in user.data" :key="use.id">
<td>{{ use.name }}</td>
<td>
{{ _.map(use.item, 'name').join(", ") }}
</td>
</tr>
`

Resources