Bilal Arshad
Bilal Arshad

Reputation: 571

How to solve "Trying to get property 'name' of non-object" error in laravel Data Tables?

I am using data tables in laravel here I am trying to fetch data with relationships some times relationship id is not present so it gives null value in laravel blade I can handle it by checking if it is null. but in the data table, I can't use it.

returning values to data table

return [

                    'delivery_run_id' =>  $run_sheet->delivery_runs->name: 

 ];

when i put before return statement then it work fine but i think that approach is not good

 if(empty($run_sheet->delivery_runs->name))
             {
                $delivery_run = '';
             }
             else
             {
              $delivery_run =  $run_sheet->delivery_runs->name;
             }

I try to set default value for data table column but its not working

 var table = $(tablename).DataTable({
        responsive: true,
        "order": [],
        'aoColumnDefs': [{
            'bSortable': false,
            'aTargets': ['nosort']
        }],
        buttons: [
            'print',
            'copyHtml5',
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5',
        ],
        processing: true,
        serverSide: true,
        ajax: {
            url: url,
            type: 'get',
            data: filters
        },

        columns: columns,


        "language": {
            processing: "<img src='https://thumbs.gfycat.com/WatchfulSnarlingBettong-max-1mb.gif'>"
        },
        "columnDefs": [
            {
              "data": null,
              "defaultContent": "<button>Edit</button>",
              "targets": -1
            }
          ]
    });

Upvotes: 1

Views: 1929

Answers (1)

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

use a ternary operator to check is set or not.

$delivery_run = $run_sheet->delivery_runs->name ?? "";
return [
    'delivery_run_id' =>  $run_sheet->delivery_runs->name ?? ""; 
];

Upvotes: 4

Related Questions