How can I get the Laravel relationship data in my columns?

How can I get the Laravel relationship data in my columns?

FoxRocks76FoxRocks76 Posts: 4Questions: 2Answers: 0
edited May 2023 in Free community support

I've got this in my Laravel 5.1 blade view:

    $(document).ready( function () {
        $('#purchase_orders_table').DataTable( {
            processing: true,
            serverSide: true,
            ajax: "{{ route('purchase-orders.list') }}",
            columns: [
                {data: 'purchase_order_number', name: 'purchase_order_number'},
                {data: 'created_at', name: 'created_at'},
                {   
                    data: 'date_expected', 
                    name: 'date_expected',
                    orderable: true, 
                    searchable: true
                },
            ]
        });
    });

And this in my controller:

    public function getPurchaseOrders(Request $request, PurchaseOrder $purchase_orders)
    {
        if ($request->ajax()) {
            $data = $purchase_orders->withTrashed()->get();
            Log::info($data->first()->vendor->name);
            return DataTables::of($data)->addIndexColumn()->make(true);
        }
    }

I would like to add the Vendor name to my table. This is accessed through a one-to-one relationship on the Purchase Order model. I'm able to confirm through my log file that the relationship is accessible but I can't figure out how to get it into my view.

I would also like to know how to access the Purchase Order model properties, such as $purchase_orders->unique_number which is generated by a getUniqueNumberAttribute() method on the model.

This is the tutorial I was following to get from using client side to server side processing:

https://dev.to/dalelantowork/laravel-8-datatables-server-side-rendering-5-easy-steps-5464

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,454Questions: 1Answers: 10,254 Site admin
    Answer ✓

    You probably need to ask here I'm afraid. The Yajra software is not published by us, so I'm afraid I can't provide any technical support for it as I don't know its capabilities in depth.

    Allan

  • FoxRocks76FoxRocks76 Posts: 4Questions: 2Answers: 0

    Thanks for the reply. I don't think I actually added the Yajra software, I just used that tutorial to help me understand the server side processing that Datatables has. I think all my code is what came with Datatables

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 394

    I don't think I actually added the Yajra software

    Did you or didn't you? That's the place to start.

  • FoxRocks76FoxRocks76 Posts: 4Questions: 2Answers: 0

    Sorry, that was a dumb answer I gave. Yes, I just checked and I do have that installed so I'll refer to their documentation.

Sign In or Register to comment.