{"action":"opened","issue":{"url":"https://api.github.com/repos/yajra/laravel-datatables/issues/586","repository_url":"https://api.github.com/repos/yajra/laravel-datatables","labels_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/586/labels{/name}","comments_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/586/comments","events_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/586/events","html_url":"https://github.com/yajra/laravel-datatables/issues/586","id":157817521,"number":586,"title":"Search: Retrieving attribute via id ","user":{"login":"Mylo-92","id":7414783,"avatar_url":"https://avatars.githubusercontent.com/u/7414783?v=3","gravatar_id":"","url":"https://api.github.com/users/Mylo-92","html_url":"https://github.com/Mylo-92","followers_url":"https://api.github.com/users/Mylo-92/followers","following_url":"https://api.github.com/users/Mylo-92/following{/other_user}","gists_url":"https://api.github.com/users/Mylo-92/gists{/gist_id}","starred_url":"https://api.github.com/users/Mylo-92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mylo-92/subscriptions","organizations_url":"https://api.github.com/users/Mylo-92/orgs","repos_url":"https://api.github.com/users/Mylo-92/repos","events_url":"https://api.github.com/users/Mylo-92/events{/privacy}","received_events_url":"https://api.github.com/users/Mylo-92/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-06-01T03:44:24Z","updated_at":"2016-06-01T03:44:24Z","closed_at":null,"body":"### Summary of problem or feature request\r\nOne-To-Many Relationship asserted via storing id of owning entity in table of many entity. E.g.: coordinator manages one or many centers. After setting the columns via the html builder, I edit the coodinator_id column and apply an accessor method to retrieve the coordinators full name:\r\n```\r\npublic function getFullNameAttribute()\r\n{\r\n    return $this->first_name . \", \" . $this->last_name;\r\n}\r\n```\r\nMy aim is to allow only the centers that belong to the authenticated coordinator to be displayed on the datatable unless the authenticated user is an admin as shown in my queries.\r\nHowever, if I enter the value of the Coordinator column that appears on the datatable it says no matching records found.\r\n\r\nHow do I achieve this?\r\n\r\n<!-- Please describe your problem/feature request here. -->\r\n\r\n### Code snippet of problem\r\n\r\n<!--\r\nIf applicable, please include a copy of your code\r\nwhich triggers the suspected bug.\r\n\r\nYou may use the markdown php code tags to format your paste: -->\r\n\r\n```\r\n<?php\r\n\r\nnamespace App\\DataTables;\r\nuse Auth;\r\nuse App\\Center;\r\nuse App\\Coordinator;\r\nuse Yajra\\Datatables\\Services\\DataTable;\r\n\r\nclass CentersDataTable extends DataTable\r\n{\r\n    // protected $printPreview  = 'path.to.print.preview.view';\r\n\r\n    /**\r\n     * Display ajax response.\r\n     *\r\n     * @return \\Illuminate\\Http\\JsonResponse\r\n     */\r\n    public function ajax()\r\n    {\r\n        return $this->datatables\r\n            ->eloquent($this->query())\r\n            ->editColumn('action', function ($center) {\r\n                return '<a href=\"edit-center/'.$center->id.'\" <i class=\"material-icons sideicons-tables\">create</i>'; \r\n               // <a href=\"field-technician-delete/'.$user->id.'\" <i class=\"material-icons sideicons-tables\">delete</i>\r\n            })\r\n            ->editColumn('coordinator_id', function ($user) {\r\n                return Coordinator::find($user->coordinator_id)->full_name;\r\n                })\r\n            ->editColumn('gps_lat', function ($center) {\r\n                    return $center->gps_location ;\r\n                })\r\n            ->make(true);\r\n    }\r\n\r\n    /**\r\n     * Get the query object to be processed by datatables.\r\n     *\r\n     * @return \\Illuminate\\Database\\Query\\Builder|\\Illuminate\\Database\\Eloquent\\Builder\r\n     */\r\n    public function query()\r\n    {\r\n        if(Auth::user()->hasRole('admin')){\r\n        $centers = Center::query();\r\n        return $this->applyScopes($centers);\r\n        }\r\n        else{\r\n            $centers = Center::query()->where('coordinator_id',Auth::user()->userable->id);\r\n            return $this->applyScopes($centers);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Optional method if you want to use html builder.\r\n     *\r\n     * @return \\Yajra\\Datatables\\Html\\Builder\r\n     */\r\n    public function html()\r\n    {\r\n        return $this->builder()\r\n             ->columns([\r\n                'id' => ['title' => 'ID'],\r\n                'center_name' => ['title' => 'Name'], \r\n                'gps_lat' => ['title' => 'Coordinates'],\r\n                'contact_no' => ['title' => 'Contact Number'],\r\n                'coordinator_id' => [\r\n                            'title' => 'Coordinator',\r\n                            'searchable' => true\r\n                ],\r\n                'created_at' => [ 'title' => 'Created'],\r\n                 'updated_at' => ['title' => 'Updated'], \r\n                'action' => [\r\n                             'data'           => 'action',\r\n                            'name'           => 'action',\r\n                            'render'         => null,\r\n                            'orderable'      => false,\r\n                            'searchable'     => false,\r\n                            'exportable'     => false,\r\n                            'printable'      => false\r\n                             ]\r\n             ])\r\n             ->parameters([\r\n                 'dom' => 'Bfrtip',\r\n                 'buttons' => ['csv', 'excel', 'print','reload'],\r\n             ]);\r\n    }\r\n\r\n    /**\r\n     * Get columns.\r\n     *\r\n     * @return array\r\n     */\r\n    private function getColumns()\r\n    {\r\n        return [\r\n            'id',\r\n            // add your columns\r\n            'created_at',\r\n            'updated_at',\r\n        ];\r\n    }\r\n\r\n    /**\r\n     * Get filename for export.\r\n     *\r\n     * @return string\r\n     */\r\n    protected function filename()\r\n    {\r\n        return 'centers';\r\n    }\r\n}\r\n```\r\n\r\n### System details\r\n\r\n<!--\r\nPlease include these details about your system!\r\nIf they are omitted, the ticket will be deprioritized\r\nover other users requests/tickets.\r\n-->\r\n\r\n- Operating System : OS X El Capitan 10.11.5\r\n- PHP Version : 7.0.0\r\n- Laravel Version : 5.1.36 (LTS)\r\n- Laravel-Datatables Version : 6.11.5\r\n\r\nRegards,\r\n"}}
{"actor":{"display_login":"Mylo-92"}}