<?php
namespace App\Http\Traits;
trait DatatableTrait {
    protected function datatable ($model, $request, $with = null) {
        if (isset($with)) {
            $model = $model->with($with);
        }
        if ($request->has('order') && $request->has('order-column')) {
            $model = $model->orderBy($request->input('order-column'), $request->order);
        } else {
            $model = $model->orderBy("id", "desc");
        }
        if ($request->has('search') && $request->has('search-columns')) {
            $search = $request->search;
            $columns = json_decode($request->input('search-columns'));
            $model = $model->where(function ($query) use ($columns, $search) {
                foreach ($columns as $column) {
                    $query->orWhere($column, 'LIKE', '%' . $search . '%');
                }
            });
        }
        return $model->paginate(10);
    }
}