Skip to content

From Small to BIGGER

Menu
  • Home
  • About Us
  • Portofolio
  • Contact
Menu

Laravel & Datatables – Server Side Manualy

Posted on April 17, 2024April 17, 2024 by kura

index.blade.php

<div class="table-responsive">
        <table class="table table-hover text-nowrap" id="myTable" style="width: 100%;">
            <thead>
                <tr>
                    <th>Fullname</th>
                    <th>Email</th>
                    <th>Handphone</th>
                    <th>Position</th>
                    <th>Pengalaman</th>
                    <th>Data Pendukung</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>

index.js

$(document).ready(function () {
    // Set up AJAX headers
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
        }
    });

    // Initialize DataTable
    const table = $('#myTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: 'https://project.local',
            data: function (d) {
                d.job_id = $('#job_id').val();
                d.job_experience_id = $('#job_experience_id').val();
                d.experience = $('#experience').val();
            },
        },
        columns: [
            { data: 'fullname' },
            { data: 'email' },
            { data: 'handphone' },
            { data: 'name' },
            { data: 'experience' },
        ],
    });

    // Filter action
    $('#btnFilter').click(function (e) {
        e.preventDefault();
        table.ajax.reload();
    });
});

Controller.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class CandidateController extends Controller
{

    public function lists(Request $request)
    {
        $job_id = $request->job_id ?? NULL;
        $job_experience_id = $request->job_experience_id ?? NULL;
        $experience = $request->experience ?? 1;

        $search = $request->input('search.value');
        $draw = $request->input('draw');

        $recordsPerPage = $request->input('length') ?? 10;
        $page = $request->input('start') ?? 0;
        $currentpage = isset($page) ? $page : 1; 

        $query = \App\Models\CandidateApply::latest('submit_at')
            ->whereRelation('candidate', 'status', 0)
            ->when($search, function (Builder $builder) use ($search) {
                $builder
                    ->whereRelation('candidate', 'fullname', 'like', "%{$search}%")
                    ->orWhere(function ($query) use ($search) {
                        $query
                            ->whereRelation('candidate', 'status', 0)
                            ->whereRelation('candidate', 'email', 'like', "%{$search}%");
                    })
                    ->orWhere(function ($query) use ($search) {
                        $query
                            ->whereRelation('candidate', 'status', 0)
                            ->whereRelation('candidate', 'handphone', 'like', "%{$search}%");
                    })
                    ->orWhere(function ($query) use ($search) {
                        $query
                            ->whereRelation('candidate', 'status', 0)
                            ->whereRelation('job', 'name', 'like', "%{$search}%");
                    });
            })
            ->when($job_id, function (Builder $builder) use ($job_id) {
                $builder->where('job_id', $job_id);
            })
            ->when($job_experience_id, function ($query, $job_experience_id) use ($experience) {
                $query->whereHas('candidate_job_experiences', function (Builder $builder) use ($experience, $job_experience_id) {
                    $builder
                        ->where('experience', $experience)
                        ->where('job_experience_id', $job_experience_id);
                });
            })
            ->when($experience, function ($query, $experience) use ($job_experience_id) {
                if (! $job_experience_id) {
                    $query->where('experience', $experience);
                }
            })
            ->with(['job', 'candidate_files']);

        $count = $query->count();
        $data = $query
            ->offset($currentpage)
            ->limit($recordsPerPage)
            ->get();

        return response()->json([
            'draw' => $draw,
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'data' => $data,
        ]);
    }

}
FacebookTweetLinkedIn

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Laravel & Datatables – Server Side Manualy
  • Linux – User Login Set Directory & Set Permission
  • Javascript Call Function From iFrame
  • Python – Create data dummy attendance
  • PHPWord Convert HTML + PDF

Categories

  • Apache
  • Database
  • Freeradius
  • Javascript
  • Mikrotik
  • Mysql
  • Networking
  • PHP
  • Postgresql
  • Programming
  • Python
  • Server
©2025 From Small to BIGGER | Built using WordPress and Responsive Blogily theme by Superb