Monday, February 27, 2017

Laravel and DataTables Editor not passing JSON data

don't mind the naming of the tables and functions. I need some help. I am using Laravel framework for the first time and I am trying to incorporate DataTables Editor plugin into it. For the most part, I can see it has been successful with integrating. My problem now is that I can't call the $db Editor variable from inside a model and pass it to a controller. I tried a number of ways and no luck. I know somebody out here has done it and can help me with that.
I have tried passing just JSON data using Laravel response()->json but Editor keeps giving an error incorrect JSON format. Also when I load the page with the server code for Editor not included in a class, it shows all the JSON data I need. I just can't get it to work in a function and pass to JS. Any help will do.
Thanks.
PS. I am learning Laravel and MVC on the fly. So don't bash me to hard :D
Oooo and one more thing. When using parent::db() it gives this error:
Call to a member function transaction() on null
Code is below.
This is my model EmployeesData.php
class EmployeesData extends Editor
{

  public function allEmployees(){
    $editor = Editor::inst(parent::db(), 'somename' )
        ->fields(
            Field::inst( 'firstname' ),
            Field::inst( 'middlename' ),
            Field::inst( 'lastname' )
        )
        ->process( $_POST )
        ->json();
}
}

This is my controller EmployeesDataAjax.php
use App\EmployeesData;


class EmployeesDataAjax extends Controller
{
    public function getEmployees() {
        $employees = new EmployeesData();
        return $employees->allEmployees();
    }
}

This is my view index.blade.php
<script>
    $(function(){

        var editor = new $.fn.dataTable.Editor( {
            ajax:  '/employees-data',
            table: '#employees-table',
            fields: [
                { label: 'First Name', name: 'firstname' },
                { label: 'Last Name',  name: 'lastname'  }
                // etc
            ]
        });

        $('#employees-table').DataTable( {
            ajax: '/employees-data',
            dom: 'Bfrtip',
            columns: [
                { data: 'firstname' },
                { data: 'lastname' }
                // etc
            ],
            select: true,
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit',   editor: editor },
                { extend: 'remove', editor: editor }
            ]
        } );

    });
</script>

Lastly, this is my route
Route::get('/employees-data', 'EmployeesDataAjax@getEmployees');



from Latest question asked on Laravel tag.


via kooliebwoy

Advertisement