How To Trim A String In A Yajra Datatable Used A Raw Query In Laravel

I have a problem on how to trim string in my datatable from a query where in used a raw query to get data from the database.

my query example is

public function data()
        return DB::table('query_table')
                DB::raw('query_table.data1 AS data1'),
                DB::raw('query_table2.data2 AS data2'),
            ->where(['' => 1])

from controller for my data table

public function dataDataTable()
        $data = $this->query->data(); //query of data

        return DataTables::of($data)

I am using datatables as view in laravel

@extends('layouts.main' , ['title' => trans('')])



        <div class="card">
            <div class="card-body">
                <header class="card-header card-header-custom">
                    <h2 class="card-title" style="color: white" >{!! trans('label.table.header') !!}</h2>
                <div class="" id="list-all">
                    <table class="display" id="templates-table">


        <script  type="text/javascript">

                processing: true,
                serverSide: true,

                ajax: '{!! route('query.datatable.route') !!}',
                type: 'get',
                columns: [
                    {data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false, width: '5%'},
                    {data: 'data1', name: 'data1', orderable: false, searchable: true, width: '65%'},
                    {data: 'data2', name: 'data2', orderable: false, searchable: true, width: '35%'},      


How do I trim data1 string so as can be seen with few characters in my datatable view ?



You can either do it in PHP or in Javascript:


    columnDefs: [{
        targets: 1,
        render: function (data, type, row) {
            return type === 'display' && data.length > 50 ? data.substr(0, 50) + '…' : data;

You can read more about it here.


use Illuminate\Support\Str;

public function dataDataTable()
    $data = $this->query->data(); // query of data

    return DataTables::of($data)
        ->editColumn('data1', function ($data) {
            return Str::limit($data->data1, 50);

If you don't have to show the user the full string I would use the PHP version, so your response does not get bloated.