MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Bagaimana saya bisa menghitung hari ini berdasarkan bulan di laravel?

Ini adalah fungsi pengontrol Anda

public function employeeAttendance(Request $request)
{
    $employeeName = User::all();
    $countMonth   = (!empty($request->get('month')) ? intval($request->get('month')) : 0);
    $countYear    = (!empty($request->get('year')) ? intval($request->get('year')) : 0);
    $firstDay     = new DateTime(date('F jS Y h:i:s A', strtotime('first day of ' . date('Y-m', strtotime("$countYear-$countYear")))));
    $lastDay      = new DateTime(date('F jS Y h:i:s A', strtotime('last day of ' . date('Y-m', strtotime("$countYear-$countYear")))));

    $employeeLeave = LeaveManagement::where('yourdatefield', '>=', $firstDay)
        ->where('yourdatefield', '<=', $lastDay)->get();
    $totalWorkingDays = 0;
    if (!empty($countMonth && $countYear)) {
        $totalWorkingDays = $this->countDays($countYear, $countMonth, array(0, 6));
    }
    $result = [];
    foreach ($employeeLeave as $empleave) {
        if ($empleave->type == 'casual') {
            $result[$empleave->name]['casual'][] = $empleave;
        }
        if ($empleave->type == 'medical') {
            $result[$empleave->name]['medical'][] = $empleave;
        }
    }
    return view('pages.attendance', compact('employeeName', 'totalWorkingDays', 'employeeLeave','result'));
}



public function countDays($year, $month, $ignore)
{
    $count   = 0;
    $counter = mktime(0, 0, 0, $month, 1, $year);
    while (date("n", $counter) == $month) {
        if (!in_array(date("w", $counter), $ignore)) {
            $count++;
        }
        $counter = strtotime("+1 day", $counter);
    }
    return $count;
}

Saya telah mengubah sedikit forloop

for ($year = 2000; $year <= 2050; $year++) {
    $selected = (isset($getYear) && $getYear == $year) ? 'selected' : '';
    echo "<option value='$year' $selected>$year</option>";
}

Istirahat semuanya diurus di pertanyaan sebelumnya, periksa apakah berhasil.

EDIT

Buat larik hasil setelah mengambil data untuk $employeeLeave

$result = [];
foreach ($employeeLeave as $empleave) {
    if ($empleave->type == 'casual') {
        $result[$empleave->name]['casual'][] = $empleave;
    }
    if ($empleave->type == 'medical') {
        $result[$empleave->name]['medical'][] = $empleave;
    }
}

kirim $result untuk memotong.

Ubah struktur tabel html Anda seperti di bawah ini,

<table class="table table-bordered table-striped ">
    <thead>
        <tr>
            <th>Employee Name</th>
            <th>Present Day</th>
            <th>Casual Leave</th>
            <th>Medical Leave</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $empname => $empleave)
            <tr>
                <td>{{ $empname }}</td>
                <td>{{ $totalWorkingDays }}</td>
                <td>{{ count($result[$empname]['casual']) }}</td>
                <td>{{ count($result[$empname]['medical']) }}</td>

            </tr>    
        @endforeach
    </tbody>
</table>



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Baca BSON (mongoDB) ke dalam POJO menggunakan GSON dan TypeAdapter

  2. Menambahkan/Mengurangi hari ke ISODate di MongoDB Shell

  3. bidang $concat dengan indeks di $map mongodb?

  4. Luwak findOneAndUpdate Upsert _id null?

  5. Bagaimana cara mendapatkan catatan N terakhir di mongodb?