1

I'm using Yajra Datatables for Laravel and it won't show the custom attribute for my User model. This is my User model:

protected $appends = ['sum_work_hours'];

public function work_hours()
{
    return $this->hasMany(WorkHour::class);
}

public function getSumWorkHoursAttribute()
{
    return $this->work_hours->sum('hours_total');
}

And this is in Controller:

public function showHours()
{
    return view('hour');
}

public function getHoursDatatable()
{
    $users = User::select(['name', 'email', 'sum_work_hours']);

    return Datatables::of($users)->make();
}

And in view:

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css">


<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">All Working Hours</div>

                <div class="panel-body">

                    <table id="users-table" class="table table-striped table-bordered">

                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Sum Work Hours</th>
                        </tr>
                        </thead>

                    </table>

                </div>
            </div>
        </div>
    </div>
</div>


<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
    $(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: 'http://127.0.0.1:8000/datatables/get_hours_datatable',
            columns: [
                { data: 'name' },
                { data: 'email' },
                { data: 'sum_work_hours' }
            ]
        });
    });
</script>

And this is what I get:

enter image description here

What could be wrong?

2
  • This column 'sum_work_hours' doesn't exist in DB table and it's probably because of that... but how to fix this? Commented Sep 3, 2017 at 19:49
  • OK It seems to me that I have fixed it....but this query takes about 7 seconds to render the table... Commented Sep 3, 2017 at 20:13

1 Answer 1

4

The problem was in my Controller

public function getHoursDatatable()
{
    // this line was wrong
    $users = User::select(['name', 'email', 'sum_work_hours']);

    return Datatables::of($users)->make();
}

It should have been like this:

$users = User::with('work_hours')->get();

Now I get the results in my table.

Sign up to request clarification or add additional context in comments.

2 Comments

all custom attributes registered in the $appends array inside a Model will automatically be loaded as an attribute. so you can do $appends = ['sum_work_hours']; in your User Model.
I already did that and this is my next problem: stackoverflow.com/questions/46027556/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.