没有收到laravel数据表中的数据

时间:2018-08-08 02:12:20

标签: php laravel laravel-5

我正在尝试使用Laravel中的数据表来获取数据。我的

view是:

<script type="text/javascript">
    var oTable;
    $(document).ready(function () {
        oTable = $('#data').DataTable({
            "processing": true,
            "serverSide": true,
            "order": [],
            "columns":[
                    {"data":"start_date"},
                    {"data":"end_date"},
                    {"data":"client_id"},
                    {"data":"created_at"},
                    {"data":"actions"}
            ],
            "ajax": "{{ url('contract') }}" + ((typeof $('#data').attr('data-id') != "undefined") ? "/" + $('#id').val() + "/" + $('#data').attr('data-id') : "/data")
        });
    });

    function deleteClient(id){

    }
</script>


 public function data(Datatables $datatables)
{

    $contObj = new Contract;
    $conttt = $contObj->get()
        ->map(function ($conttt) {
        return [
            'id' => $conttt->id,
            'start_date' => $conttt->start_date,
            'end_date' => $conttt->end_date,
          'client_id' => $conttt->user_id,
            'created_at' => $conttt->created_at
        ];
    });

    return $datatables->collection($conttt)

        ->addColumn('actions', '@if(Sentinel::inRole(\'admin\'))
                                <a href="{{ url(\'contract/\' . $id . \'/edit\' ) }}" title="{{ trans(\'table.edit\') }}">
                                        <i class="fa fa-fw fa-pencil text-warning "></i> </a>
                                @endif
                                <a href="{{ url(\'contract/\' . $id . \'/show\' ) }}" title="{{ trans(\'table.details\') }}" >
                                        <i class="fa fa-fw fa-eye text-primary"></i> </a>
                               ')

        ->removeColumn('id')
        ->rawColumns(['actions'])->make();
}

但是当我得到的数据是:

"data":[{"start_date":null,"end_date":null,"client_id":"3","created_at":{"date":"2018-08-07 10:13:46.000000","timezone_type":"3","timezone":"UTC"},"actions":"                                    <a href=\"http:\/\/billing.mj.milesweb.cloud\/files\/public\/contract\/1\/edit\" title=\"Edit\">\n                                            <i class=\"fa fa-fw fa-pencil text-warning \"><\/i> <\/a>\n                                                                        <a href=\"http:\/\/billing.mj.milesweb.cloud\/files\/public\/contract\/1\/show\" title=\"Show\" >\n                                            <i class=\"fa fa-fw fa-eye text-primary\"><\/i> <\/a>\n                                   "}

当我在数据表中看到此数据时,我在字段created_at中看到显示的数据为[object Object],而在json中则可以。请帮助我在数据表中显示正确的数据

4 个答案:

答案 0 :(得分:0)

这是因为created_at是Carbon对象,您需要一个字符串。对于“ created_at”,请尝试返回以下内容:

\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $conttt->created_at)->format('m/d/y h:i A')

答案 1 :(得分:0)

默认情况下,created_at和Updated_at是Carbon类对象。您需要将其转换为自己的格式:

$conttt = $contObj->get()
    ->map(function ($conttt) {
    return [
        'id' => $conttt->id,
        'start_date' => $conttt->start_date,
        'end_date' => $conttt->end_date,
      'client_id' => $conttt->user_id,
        'created_at' => $conttt->created_at->format('d-m-Y')
    ];
});

您还可以将此代码编写到模型类中。为此,您需要使用Laravel mutators。 祝你好运!

答案 2 :(得分:0)

您可以像这样在数据表中编辑列:

return Datatables::of($conttt)
            ->editColumn('created_at', function($conttt) {
                return $conttt->created_at->format('d M Y H:i:s');
            })
            ->make(true);

答案 3 :(得分:0)

尝试

1)

$contObj = new Contract;
$conttt = $contObj->get()
    ->map(function ($conttt) {
    return [
        'id' => $conttt->id,
        'start_date' => $conttt->start_date,
        'end_date' => $conttt->end_date,
      'client_id' => $conttt->user_id,
        'created_at' => date('Y-m-d h:s:i', strtotime($conttt->created_at))
    ];
});

2)

$contObj = Contract::all();