Laratables
  • Package Source
Basic Customize Column One-To-One One-To-Many Many-To-Many One-To-Many Polymorphic

Basic

Customize Column

One to One

One To Many

Many To Many

One To Many Polymorphic

Screenshots Controller View Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
            * Display the Home Page.
            *
            * @return  \Illuminate\Http\Response
            **/
        public function index()
        {
            return view('basic_laratable');
        }

        /**
            * Returns data of the basic datatables.
            *
            * @return  Illuminate\Http\JsonResponse
            **/
        public function basicLaratableData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="basic-laratable" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Start Date</th>
                        <th>Salary</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#basic-laratable").DataTable({
                serverSide: true,
                ajax: "{{ route('basic_laratable') }}",
                columns: [
                    { name: 'first_name' },
                    { name: 'last_name' },
                    { name: 'start_date' },
                    { name: 'salary' },
                ],
            });
        });
    

First Name Last Name Start Date Salary
Screenshots Controller View Model Config Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
         * Display the Home Page.
         *
         * @return  \Illuminate\Http\Response
         **/
        public function index()
        {
            return view('custom_laratable');
        }

        /**
         * Returns data of the custom datatables.
         *
         * @return  Illuminate\Http\JsonResponse
         **/
        public function customLaratableData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="custom-laratable" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>Serial No</th>
                        <th>Name</th>
                        <th>Start Date</th>
                        <th>Salary</th>
                        <th>Action</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#custom-laratable").DataTable({
                serverSide: true,
                ajax: "{{ route('custom_laratable') }}",
                order: [[1, "asc"]],
                columns: [
                    { name: 'serial_no', orderable: false, searchable: false},
                    { name: 'Name' },
                    { name: 'start_date' },
                    { name: 'salary' },
                    { name: 'action', orderable: false, searchable:false},
                ],
            });
        });
    
    
        /**
         * The attributes that should be mutated to dates.
         *
         * @var  array
         */
        protected $dates = [
            'start_date',
        ];

        /**
         * Display currency symbol with format in salary column value.
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesSalary($user)
        {
            return "$".number_format($user->salary);
        }

        /**
         * Adds the condition for searching the salary if custom/modify for display.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchSalary($query, $searchValue)
        {
            if ($searchSalary = filter_var($searchValue, FILTER_SANITIZE_NUMBER_INT)) {
                return $query->orWhere('salary', 'like', '%'. $searchSalary. '%');
            }

            return $query;
        }

        /**
         * Returns the first_name & last_name value in Name column for datatables.
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesCustomName($user)
        {
            return $user->first_name.' '.$user->last_name;
        }

        /**
         * Additional merged columns to be loaded for datatables.
         *
         * @return  array
         */
        public static function laratablesAdditionalColumns()
        {
            return ['first_name', 'last_name'];
        }

        /**
         * First_name column should be used for sorting when Name column is selected in Datatables.
         *
         * @return  string
         */
        public static function laratablesOrderName()
        {
            return 'first_name';
        }

        /**
         * Searching the user(column merged) in the query.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @param  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchName($query, $searchValue)
        {
            return $query->orWhere('first_name', 'like', '%'. $searchValue. '%')
                ->orWhere('last_name', 'like', '%'. $searchValue. '%')
            ;
        }

        /**
         * Returns the action Column html for datatables.
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesCustomAction($user)
        {
            return view('action', compact('user'))->render();
        }

        /**
         * Sets the serial number column value for each user.
         *
         * @param  \Illuminate\Support\Collection
         * @return  \Illuminate\Support\Collection
         **/
        public static function laratablesModifyCollection($users)
        {
            return $users->map(function ($user, $key) {
                $user->serial_no = $key + 1 + request()->input('start');
                return $user;
            });
        }

        /**
         * Returns the custom column serial number value for table.
         *
         * @param  \App\User $user
         * @return  string
         **/
        public static function laratablesCustomSerialNo($user)
        {
            return $user->serial_no;
        }
    
    
        // Display format of dates (carbon instances) of the table.
        'date_format' => 'd-m-Y',
    

Serial No Name Start Date Salary Action
Screenshots Controller View Model Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
         * Display the Home Page.
         *
         * @return  \Illuminate\Http\Response
         **/
        public function index()
        {
            return view('one_to_one');
        }

        /**
         * Returns data of the One To One Relationship datatables.
         *
         * @return  Illuminate\Http\JsonResponse
         **/
        public function oneToOneData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="one-to-one-laratable" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Start Date</th>
                        <th>Salary</th>
                        <th>Country Name</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#one-to-one-laratable").DataTable({
                serverSide: true,
                ajax: "{{ route('one_to_one') }}",
                columns: [
                    { name: 'first_name' },
                    { name: 'last_name' },
                    { name: 'start_date' },
                    { name: 'salary' },
                    { name: 'country.name', orderable: false},
                ],
            });
        });
    
    
        /**
         * Get the Country that owns the users.
         **/
        public function country()
        {
            return $this->belongsTo('App\Country');
        }
    

First Name Last Name Start Date Salary Country Name
Screenshots Controller View Model Config Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
         * Display the Home Page.
         *
         * @return  \Illuminate\Http\Response
         **/
        public function index()
        {
            return view('one_to_many');
        }

        /**
         * Returns data of the One To Many Relationship datatables.
         *
         * @return  Illuminate\Http\JsonResponse
         **/
        public function oneToManyData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="one-to-many" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>Name</th>
                        <th>Start Date</th>
                        <th>Salary</th>
                        <th>User Comments</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#one-to-many").DataTable({
                serverSide: true,
                ajax: "{{ route('one_to_many') }}",
                columns: [
                    { name: 'Name' },
                    { name: 'start_date' },
                    { name: 'salary' },
                    { name: 'user_comments',  orderable: false },
                ],
            });
        });
    
    
        /**
         * Get the comments for the User.
         */
        public function comments()
        {
            return $this->hasMany('App\Comment');
        }

        /**
         * Eager load comments of the user.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesQueryConditions($query)
        {
            return $query->with('comments');
        }

        /**
         * Display the relationship data in custom column(user_comments).
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesCustomUserComments($user)
        {
            return $user->comments->implode('content', ',');
        }

        /**
         * Adds the condition for searching the User comment.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchUserComments($query, $searchValue)
        {
            return $query->orWhereHas('comments', function ($query) use ($searchValue) {
                $query->where('content', 'like', "%". $searchValue ."%");
            });
        }

        /**
         * Display currency symbol with format in salary column value.
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesSalary($user)
        {
            return "$".number_format($user->salary);
        }

        /**
         * Adds the condition for searching the salary if custom/modify for display.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchSalary($query, $searchValue)
        {
            if ($searchSalary = preg_replace('/[^A-Za-z0-9\-]/', '', $searchValue)) {
                return $query->orWhere('salary', 'like', '%'. $searchSalary. '%');
            }

            return $query;
        }

        /**
         * The attributes that should be mutated to dates.
         *
         * @var  array
         */
        protected $dates = [
            'start_date',
        ];

        /**
         * Returns the first_name & last_name value in Name column for datatables.
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesCustomName($user)
        {
            return $user->first_name. ' ' .$user->last_name;
        }

        /**
         * Additional merged columns to be loaded for datatables.
         *
         * @return  array
         */
        public static function laratablesAdditionalColumns()
        {
            return ['first_name', 'last_name'];
        }

        /**
         * First_name column should be used for sorting when Name column is selected in Datatables.
         *
         * @return  string
         */
        public static function laratablesOrderName()
        {
            return 'first_name';
        }

        /**
         * Searching the user(column merged) in the query.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @param  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchName($query, $searchValue)
        {
            return $query->orWhere('first_name', 'like', '%'. $searchValue. '%')
                ->orWhere('last_name', 'like', '%'. $searchValue. '%')
            ;
        }
    
    
        // Display format of dates (carbon instances) of the table.
        'date_format' => 'd-m-Y',
    

Name Start Date Salary User Comments
Screenshots Controller View Model Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
         * Display the Home Page.
         *
         * @return  \Illuminate\Http\Response
         **/
        public function index()
        {
            return view('many_to_many');
        }

        /**
         * Returns data of the Many To Many Relationship datatables.
         *
         * @return  Illuminate\Http\JsonResponse
         **/
        public function manyToManyData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="many-to-many" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Start Date</th>
                        <th>Salary</th>
                        <th>User Roles</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#many-to-many").DataTable({
                serverSide: true,
                ajax: "{{ route('many_to_many') }}",
                columns: [
                    { name: 'first_name' },
                    { name: 'last_name' },
                    { name: 'start_date' },
                    { name: 'salary' },
                    { name: 'user_roles',  orderable: false },
                ],
            });
        });
    
    
        /**
         * The roles that belong to the user.
         */
        public function roles()
        {
            return $this->belongsToMany('App\Role');
        }

        /**
         * Eager load roles of the user.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesQueryConditions($query)
        {
            return $query->with('roles');
        }

        /**
         * Display the relationship data in custom column(user_roles).
         *
         * @param  \App\User
         * @return  string
         */
        public static function laratablesCustomUserRoles($user)
        {
            return $user->roles->implode('name', ',');
        }

        /**
         * Adds the condition for searching the User Roles.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @param  string search term
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesSearchUserRoles($query, $searchValue)
        {
            return $query->orWhereHas('roles', function ($query) use ($searchValue) {
                $query->where('name', 'like', "%". $searchValue ."%");
            });
        }
    

First Name Last Name Start Date Salary User Roles
Screenshots Controller View Model Output
    
        namespace App\Http\Controllers;

        use App\User;
        use Freshbitsweb\Laratables\Laratables;

        /**
         * Display the Home Page.
         *
         * @return  \Illuminate\Http\Response
         **/
        public function index()
        {
            return view('one_to_many_poly');
        }

        /**
         * Returns data of the One To Many Polymorphic Relationship datatables.
         *
         * @return  Illuminate\Http\JsonResponse
         **/
        public function oneToManyPolyData()
        {
            return Laratables::recordsOf(User::class);
        }
    
    
        <div class="container">
            <table id="one-to-many-poly" class="table table-bordered table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>Post Title</th>
                        <th>Post Image</th>
                        <th>Liked By</th>
                    </tr>
                </thead>
            </table>
        </div>
    
    
        $(document).ready(function(){
            $("#one-to-many-poly").DataTable({
                serverSide: true,
                ajax: "{{ route('one_to_many_poly') }}",
                columns: [
                    { name: 'title' },
                    { name: 'image_url', searchable: false },
                    { name: 'post_liked', orderable: false },
                ],
            });
        });
    
    
	    /**
     	 * Get all of the post's likes.
     	 */
	    public function likes()
	    {
		    return $this->morphMany('App\Like', 'likeable');
	    }

        /**
         * Eager load likes value of the post.
         *
         * @param  \Illuminate\Database\Eloquent\Builder
         * @return  \Illuminate\Database\Eloquent\Builder
         */
        public static function laratablesQueryConditions($query)
        {
            return $query->with('likes');
        }

        /**
         * Display the relationship data in custom column post_liked.
         *
         * @param  \App\Post
         * @return  string
         */
        public static function laratablesCustomPostLiked($post)
        {
            return $post->likes->implode('name', ',');
        }

        /**
         * Display image from url in table.
         *
         * @param  \App\Post
         * @return  string
         */
        public static function laratablesImageUrl($post)
        {
            return '<
img src
="$post->image_url">'; } /** * Searching the post_liked column data. * * @param \Illuminate\Database\Eloquent\Builder * @param string search term * @return \Illuminate\Database\Eloquent\Builder */ public static function laratablesSearchPostLiked($query, $searchValue) { return $query->orWhereHas('likes', function ($query) use ($searchValue) { $query->where('name', 'like', "%" .$searchValue. "%"); }); }

Post Title Post Image Liked By