Laravel-datatables: Belongs To relation (role) on user table throwing error when role_id null

Created on 15 Apr 2020  路  2Comments  路  Source: yajra/laravel-datatables

Its okay when role_id has value on database on user table

Error Alert

DataTables warning: table id=dataTableBuilder - Requested unknown parameter 'role.name' for row 1, column 2. For more information about this error, please see http://datatables.net/tn/4

User Model

public function role(){
    return $this->belongsTo('\App\Role', 'role_id');
}

UserDataTable

<?php

namespace App\DataTables;

use App\Models\User;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;

class UserDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $dataTable = new EloquentDataTable($query);

        return $dataTable->addColumn('action', 'admin.users.datatables_actions');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\User $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(User $model)
    {
        return $model->newQuery()->with(['role']);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '120px', 'printable' => false])
            ->parameters([
                'dom'       => 'Bfrtip',
                'stateSave' => true,
                'order'     => [[0, 'desc']],
                'buttons'   => [
                    ['extend' => 'create', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'export', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'print', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'reset', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'reload', 'className' => 'btn btn-default btn-sm no-corner',],
                ],
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            // 'role_id',
            'id',
            'name',
            // 'role',
            'role' => new \Yajra\DataTables\Html\Column(['title' => 'Role', 'data' => 'role.name', 'name' => 'role.name', 'searchable' => false]),
            'email',
            // 'password',
            'phone',
            // 'image_path'
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'usersdatatable_' . time();
    }
}

INFYOM-GEN

question

Most helpful comment

I think you just need to add withDefault on your relation.

return $this->belongsTo('\App\Role', 'role_id')->withDefault();

working with
return $this->belongsTo('\App\Role', 'role_id')->withDefault(['name' => '']);

All 2 comments

I think you just need to add withDefault on your relation.

return $this->belongsTo('\App\Role', 'role_id')->withDefault();

I think you just need to add withDefault on your relation.

return $this->belongsTo('\App\Role', 'role_id')->withDefault();

working with
return $this->belongsTo('\App\Role', 'role_id')->withDefault(['name' => '']);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ezani92 picture ezani92  路  33Comments

FaZeRs picture FaZeRs  路  18Comments

ruchisheth picture ruchisheth  路  16Comments

baig772 picture baig772  路  14Comments

jayenn007 picture jayenn007  路  21Comments