Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rjruiz's avatar

Laravel 5.6 DataTables server side process

Hello community based on what they have answered me I have been able to orientate myself better, I have modified my code and I still cannot show the records that I want in my datatable I have 3 tables users clients companies. My relations at the migration level.

-> users table:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id'); 

-> clients table:

public function up()
{
    Schema::create('clients', function (Blueprint $table) {            
        $table->increments('id')->unsigned();                                 
        $table->foreign('id')->references('id')->on('users')
            ->onDelete('cascade');

    });    

-> companies table:

public function up()
{
    Schema::create('companies', function (Blueprint $table) {           
        $table->increments('id')->unsigned();                 
        $table->foreign('id')->references('id')->on('clients')
            ->onDelete('cascade');

    });
}

Model level relationships:

-> user model:

public function client()
{       
    return $this->hasOne(Client::class);
} 

-> client model:

public function company()
{
    return $this->hasOne(Company::class);
}

public function user()
{
   return $this->belongsTo(User::class);
}

-> company model:

public function client()
{
   return $this->belongsTo(Client::class);
}

-> my ClientController controller:

use Illuminate\Http\Request;
use App\User;
use App\Client;
use App\Company;
use DataTables;
class ClientController extends Controller 
{        
 public function index()
 {        
    return view('clientes.customers', compact('clientes'));
 }

 public function dataTable()
 {
    $clientes = Client::with(['user.company']);

        return DataTables::eloquent($clientes)
            ->addColumn('action', function($clientes){
                $button = '<button type="button" name="edit" 
                id="'.$clientes->id.'" class="edit btn btn-primary btn- 
                sm">Edit</button>';
                $button .= '&nbsp;&nbsp;';
                $button .= '<button type="button" name="delete" 
                id="'.$clientes->id.'" class="delete btn btn-danger btn- 
                sm">Delete</button>';
                return $button;
            })
            ->rawColumns(['action'])                
            ->make(true);      
 }
}

It is correct that my client variable thus happens in my view in the index method confuses me with the traditional way.

-> my script:

<script>
$(function ($) {  

  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }          
  });      

  $('#clientes-table').DataTable({        
    responsive  : true,
    processing  : true,
    serverSide  : true,
    ajax        : "{!! route('table.clientes') !!}",                 
    columns:  [
      {data: 'id', name: 'id'},
      {data: 'user.company.registro_fiscal', name: 'user.company.companies.registro_fiscal'},
      {data: 'user.company.nombre', name: 'user.company.companies.nombre'},
      {data: 'user.company.telefono', name: 'user.company.companies.telefono'},
      {data: 'user.company.sitio_web', name: 'user.company.companies.sitio_web'},
      {data: 'direccion', name: 'clients.direccion'},
      {data: 'ciudad', name: 'clients.ciudad'},       
      {data: 'pais', name: 'clients.pais'},
      {data: 'user.company.name', name: 'user.company.users.name'},
      {data: 'telefono_contacto', name: 'clients.telefono_contacto'},
      {data: 'user.company.email', name: 'user.company.users.email'},
      {data: 'action', name: 'Action', orderable: false, searchable: false}
    ]        
  });
</script>

I am not sure if I am correctly manipulating the data in columns data-name

-> my routes:

Route::group([
'prefix' => 'customers',
'middleware' => 'auth'],
function(){    
 Route::get('/', 'ClientController@index')->name('clientes');       
 Route::get('client', 'ClientController@dataTable')->name('table.clientes');    
});

Well, I think that is all explaining me a bit but I needed that you could understand me to give me a hand, where can my mistake be, whether in relationships or in the way of passing my data to my datatable? I look forward to your prompt help. Thanks for your time!!

0 likes
1 reply
rjruiz's avatar

You are bringing this to me when you type the route manually in the browser (http: //roles.local: 8080 / customers / client):

{
 "draw": 0,
 "recordsTotal": 19,
 "recordsFiltered": 19,
 "data": [
   {
    "id": "1",
    "direccion": "Suite 881",
    "ciudad": "Johnsonland",
    "provincia": "Illinois", 
    "codigo_postal": "483",
    "pais": "Afghanistan",
    "telefono_contacto": "1-327-508-5875 x1784",
    "user": null,
    "action": "<button type=\"button\" name=\"edit\" id=\"1\" class=\"edit 
     btn btn-primary btn-sm\">Edit</button>&nbsp;&nbsp;<button 
     type=\"button\" name=\"delete\" id=\"1\" class=\"delete btn btn-danger 
     btn-sm\">Delete</button>"
   },
   {
    "id": "2",
    "direccion": "Apt. 519",
    "ciudad": "Thompsonville",
    "provincia": "Alaska", 
    "codigo_postal": "422",
    "pais": "Mayotte",
    "telefono_contacto": "1-497-415-5362 x7992",
    "user": null,
    "action": "<button type=\"button\" name=\"edit\" id=\"2\" class=\"edit 
     btn btn-primary btn-sm\">Edit</button>&nbsp;&nbsp;<button 
     type=\"button\" name=\"delete\" id=\"2\" class=\"delete btn btn-danger 
     btn-sm\">Delete</button>"
    }

Just bring me the data in the customer table for example this data {data: 'address', name: 'customers.direction'}, and where I apply my relationship does not bring them. User data is null

Please or to participate in this conversation.