Estoy implementando softdeletes con los clientes registrados en el sistema, estos clientes se relacionan con el modelo company al querer visualizar en mi datatables aquellos que han sido eliminados, solo puedo observar los datos de la tabla clients. Los datos de la tabla company no se pueden visualizar
0: {id: "6", name_client: "Elijah Schmitt", lastname: "Daugherty", address: "Suite 610",…} id: "6" name_client: "Elijah Schmitt" lastname: "Daugherty" address: "Suite 610" city: "Lake Christa" province: "Nebraska" postal_code: "11730" country: "Haiti" phone_client: "+1872884775841" email: "[email protected]" deleted_at: "18-12-19" created_at: "2002-07-07 00:00:00" updated_at: "2019-12-18 10:36:06" company: null
la relacion con company es null
controlador:
<?php
namespace App\Http\Controllers;
use DataTables;
use App\Client;
use App\Company;
use Illuminate\Http\Request;
class ClientRestoreController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.customers.restore');
}
public function dataTable()
{
$clientes = Client::onlyTrashed()->with('company')
->get();
return dataTables::of($clientes)
->addColumn('id', function ($clientes){
return $clientes->id;
})
->addColumn('cliente', function ($clientes){
return
'<i class="fa fa-industry"></i>'.' '.$clientes->company['name_company']."<br>".
'<i class="fa fa-phone"></i>'.' '.$clientes->company['phone_company']."<br>".
'<i class="fa fa-globe"></i>'.' '.$clientes->company['web'];
})
->addColumn('contacto', function ($clientes){
return
'<i class="fa fa-user"></i>'.' '.$clientes->name_client."<br>".
'<i class="fa fa-phone"></i>'.' '.$clientes->phone_client."<br>".
'<i class="fa fa-envelope"></i>'.' '.$clientes->email;
})
->addColumn('deleted_at', function ($clientes){
return $clientes->deleted_at->format('d-m-y');
})
->addIndexColumn()
->rawColumns(['cliente', 'contacto', 'deleted_at'])
->make(true);
}
}
La relacion con company es null. Porque esto no funciona:
$clientes = Client::onlyTrashed()->with('company')
->get();
ACTUALIZADO
Modificando esto:
$clientes = Client::onlyTrashed()->with('company')
->get()
a esto:
$clientes = Client::onlyTrashed()->get()->map(function ($client) {
$client->deleted_client = $client->company()->withTrashed()->first();
return $client;
});
observo en la consola lo siguiente:
0: {id: "6", name_client: "Elijah Schmitt", lastname: "Daugherty", address: "Suite 610",…}
id: "6"
name_client: "Elijah Schmitt"
lastname: "Daugherty"
address: "Suite 610"
city: "Lake Christa"
province: "Nebraska"
postal_code: "11730"
country: "Haiti"
phone_client: "+1872884775841"
email: "[email protected]"
deleted_at: "18-12-19"
created_at: "2002-07-07 00:00:00"
updated_at: "2019-12-18 10:36:06"
deleted_client: {id: "6", client_id: "6", name_company: "acerind", cuit: "20-12345432-12", web: null,…}
id: "6"
client_id: "6"
name_company: "acerind"
cuit: "20-12345432-12"
web: null
phone_company: "03424562111"
deleted_at: "2019-12-18 10:36:06"
created_at: "2019-12-18 00:00:00"
updated_at: "2019-12-18 10:36:06"
cliente: "<i class="fa fa-industry"></i> <br><i class="fa fa-phone"></i> <br><i class="fa fa-globe"></i> "
contacto: "<i class="fa fa-user"></i> Elijah Schmitt<br><i class="fa fa-phone"></i> +1872884775841<br><i class="fa fa-envelope"></i> [email protected]"
DT_RowIndex: 1
input: {draw: "1", columns: [,…], order: [{column: "0", dir: "asc"}], start: "0", length: "10",…}
Con esto logro traer la compania asociada al cliente, pero ahora no puedo acceder a esos datos?. Me refiero a esto:
->addColumn('cliente', function ($clientes){
return
'<i class="fa fa-industry"></i>'.' '.$clientes->deleted_client->company['name_company']."<br>".
'<i class="fa fa-phone"></i>'.' '.$clientes->deleted_client->company['phone_company']."<br>".
'<i class="fa fa-globe"></i>'.' '.$clientes->deleted_client->company['web'];
})