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 .= ' ';
$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!!