{"action":"created","issue":{"url":"https://api.github.com/repos/yajra/laravel-datatables/issues/808","repository_url":"https://api.github.com/repos/yajra/laravel-datatables","labels_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/808/labels{/name}","comments_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/808/comments","events_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/808/events","html_url":"https://github.com/yajra/laravel-datatables/issues/808","id":184679542,"number":808,"title":"Export Buttons not working on a non-service implementation","user":{"login":"sabival89","id":2057169,"avatar_url":"https://avatars.githubusercontent.com/u/2057169?v=3","gravatar_id":"","url":"https://api.github.com/users/sabival89","html_url":"https://github.com/sabival89","followers_url":"https://api.github.com/users/sabival89/followers","following_url":"https://api.github.com/users/sabival89/following{/other_user}","gists_url":"https://api.github.com/users/sabival89/gists{/gist_id}","starred_url":"https://api.github.com/users/sabival89/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sabival89/subscriptions","organizations_url":"https://api.github.com/users/sabival89/orgs","repos_url":"https://api.github.com/users/sabival89/repos","events_url":"https://api.github.com/users/sabival89/events{/privacy}","received_events_url":"https://api.github.com/users/sabival89/received_events","type":"User","site_admin":false},"labels":[{"id":412305485,"url":"https://api.github.com/repos/yajra/laravel-datatables/labels/not%20supported","name":"not supported","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-23T09:15:17Z","updated_at":"2016-10-28T21:59:23Z","closed_at":null,"body":"\r\n\r\nHello,\r\n\r\nI have tried the [Service Implementation](https://datatables.yajrabox.com/services/basic), and it works pretty well on exporting table contents. But whenever i try using non service implementations, the export buttons don't work. It keeps redirecting to a JSON result page. please see output below..\r\n\r\n![print-pdf-csv-excel-output](https://cloud.githubusercontent.com/assets/2057169/19625901/0c7291f6-991d-11e6-8dd0-a6ac248a94e0.PNG)\r\n\r\nKindly assist me please cuz i'm pretty stuck.\r\n\r\n## My Controller**\r\n![controller](https://cloud.githubusercontent.com/assets/2057169/19625120/563a09b0-9907-11e6-9f06-d875f3c46718.PNG)\r\n\r\n## My Script Declaration**\r\n![script](https://cloud.githubusercontent.com/assets/2057169/19625124/77f22b64-9907-11e6-8a80-46c03d75d1e5.PNG)\r\n\r\n## My View\r\n\r\n![table](https://cloud.githubusercontent.com/assets/2057169/19625142/67bad466-9908-11e6-915d-1fb92978d87e.PNG)\r\n\r\n## The Output when ever i click on any of the export buttons: Print, CSV, PDF, EXCEL\r\n![print-pdf-csv-excel-output](https://cloud.githubusercontent.com/assets/2057169/19625901/0c7291f6-991d-11e6-8dd0-a6ac248a94e0.PNG)"},"comment":{"url":"https://api.github.com/repos/yajra/laravel-datatables/issues/comments/257039159","html_url":"https://github.com/yajra/laravel-datatables/issues/808#issuecomment-257039159","issue_url":"https://api.github.com/repos/yajra/laravel-datatables/issues/808","id":257039159,"user":{"login":"turgutsaricam","id":5666595,"avatar_url":"https://avatars.githubusercontent.com/u/5666595?v=3","gravatar_id":"","url":"https://api.github.com/users/turgutsaricam","html_url":"https://github.com/turgutsaricam","followers_url":"https://api.github.com/users/turgutsaricam/followers","following_url":"https://api.github.com/users/turgutsaricam/following{/other_user}","gists_url":"https://api.github.com/users/turgutsaricam/gists{/gist_id}","starred_url":"https://api.github.com/users/turgutsaricam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/turgutsaricam/subscriptions","organizations_url":"https://api.github.com/users/turgutsaricam/orgs","repos_url":"https://api.github.com/users/turgutsaricam/repos","events_url":"https://api.github.com/users/turgutsaricam/events{/privacy}","received_events_url":"https://api.github.com/users/turgutsaricam/received_events","type":"User","site_admin":false},"created_at":"2016-10-28T21:59:23Z","updated_at":"2016-10-28T21:59:23Z","body":"You can create a base service class. Here is my implementation.\r\n\r\n```php\r\nuse Illuminate\\Contracts\\View\\Factory;\r\nuse Illuminate\\Database\\Query\\Builder;\r\nuse Yajra\\Datatables\\Datatables;\r\nuse Yajra\\Datatables\\Engines\\BaseEngine;\r\nuse Yajra\\Datatables\\Services\\DataTable;\r\n\r\nclass DataTableBase extends DataTable {\r\n\r\n    /*\r\n     * The reason why this class exists is that Yajra/Datatables requires this service class for export methods to work\r\n     * properly.\r\n     */\r\n\r\n    /** @var Builder The query that will be used to get the data from the db. */\r\n    private $mQuery;\r\n\r\n    /** @var array An array of columns */\r\n    private $mColumns;\r\n\r\n    /** @var BaseEngine The DataTable */\r\n    private $mDataTable;\r\n\r\n    /**\r\n     * @param            $query\r\n     * @param BaseEngine $dataTable\r\n     * @param array      $columns\r\n     */\r\n    public function __construct($query, BaseEngine $dataTable, $columns) {\r\n        parent::__construct(app(Datatables::class), app(Factory::class));\r\n\r\n        $this->mQuery = $query;\r\n        $this->mColumns = $columns;\r\n        $this->mDataTable = $dataTable;\r\n    }\r\n\r\n    /**\r\n     * @return \\Illuminate\\Http\\JsonResponse\r\n     */\r\n    public function ajax() {\r\n        return $this->mDataTable->make(true);\r\n    }\r\n\r\n    /**\r\n     * @return \\Illuminate\\Database\\Eloquent\\Builder|\\Illuminate\\Database\\Query\\Builder|\\Illuminate\\Support\\Collection\r\n     */\r\n    public function query() {\r\n        $this->mQuery;\r\n    }\r\n\r\n    public function html() {\r\n        return $this->builder()->columns($this->mColumns);\r\n    }\r\n    \r\n}\r\n```\r\n\r\nThen, you can use the datatable you created without a service like this:\r\n\r\n```php\r\n$base = new DataTableBase($query, $dataTable, $columns);\r\nreturn $base->render(null);\r\n```\r\n\r\nJust do not call `make` method for your datatable instance and let the `$base` handle it. So, your `userdata` method may look like this:\r\n\r\n```php\r\npublic function userdata() {\r\n    $query = User::latest('created_at');\r\n    $dataTable = Datatables::of($query); // Do not call make here\r\n    $columns = ['firstname', 'lastname', 'email', 'action'];\r\n\r\n    $base = new DataTableBase($query, $dataTable, $columns);\r\n    return $base->render(null);\r\n}\r\n```"}}
{"actor":{"display_login":"turgutsaricam"}}