If you are having problems with this
$ordenes =Client::with (['orders, orders.details'])->get();
then you need to show the Client model so we can see the orders relationship
Ideally also, your migrations.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Well, I'm having trouble showing data about my relationship with datatables. The information I want to visualize is related in the following way: I have the table customers, orders and details: a customer can have 1 to N orders. An order belongs to a customer. An order can have 1 to N details. A detail belongs to an order. Here my relationships in the requested model:
class OrderNote extends Model
{
protected $fillable = [ 'num_order', 'status', 'date', 'finished_date'];
public $timestamps = true;
protected $dates = ['date', 'finished_date'];
public function client()
{
return $this->belongsTo(Client::class);
}
public function details()
{
return $this->hasMany(OrderDetail::class);
}
through this query:
$ordenes =Client::with (['orders, orders.details'])->get();
My OrderNote controller:
<?php
namespace App\Http\Controllers;
use App\OrderNote;
use App\Client;
use App\OrderDetail;
use DataTables;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class OrderNoteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.orders.index');
}
public function dataTable()
{
$ordenes =Client::with (['orders, orders.details'])->get();
// $ordenes = OrderNote::with('client', 'details')->get();
// $ordenes = OrderNote::with('client')->select('clients.*');
// $ordenes = Client::first()->orders()->with('client');
return dataTables::of($ordenes)
->addColumn('id', function ($ordenes){
return $ordenes->id;
})
->addColumn('fecha', function ($ordenes){
return $ordenes->date->format('d-m-y');
})
->addColumn('cliente', function ($ordenes){
return
'<i class="fa fa-user"></i>'.' '.$ordenes->client->name_client."<br>".
'<i class="fa fa-phone"></i>'.' '.$ordenes->client->phone_client;
})
->addColumn('producto', function ($ordenes){
return
'<i class="fa fa-industry"></i>'.' '.$ordenes->details->name."<br>".
'<i class="fa fa-phone"></i>'.' '.$ordenes->details->code."<br>".
'<i class="fa fa-globe"></i>'.' '.$ordenes->details->description;
})
->addColumn('estado', function ($ordenes){
return $ordenes->status;
})
->addColumn('accion', function ($ordenes) {
return view('admin.orders.partials._action', [
'clientes' => $ordenes,
'url_show' => route('admin.orders.show', $ordenes->id),
'url_edit' => route('admin.orders.edit', $ordenes->id),
'url_destroy' => route('admin.orders.destroy', $ordenes->id)
]);
})
->addIndexColumn()
->rawColumns(['fecha', 'cliente', 'producto', 'estado', 'accion'])
->make(true);
}
Finally my script in my index view:
<script>
$('#datatable').DataTable({
responsive: true,
processing: true,
serverSide: true,
ajax: "{{ route('orders.table') }}",
columns: [
{data: 'id', name: 'id'},
{data: 'fecha', name: 'fecha'},
{data: 'cliente', name: 'cliente'},
{data: 'producto', name: 'producto'},
{data: 'estado', name: 'estado'},
{data: 'accion', name: 'accion'}
]
});
</script>
details is a collection, not an object. How to access each detail in the collection to obtain its attributes. How to tour the collection with eloquent and show them in my datatables?
Please or to participate in this conversation.