Datatables Editor and Laravel without yajra

Datatables Editor and Laravel without yajra

volnistii11volnistii11 Posts: 49Questions: 16Answers: 0

Please tell me how to use the datatable and editor correctly in laravel without yajra?
I tried using Yajra, I didn't really like it. Before that, in native php, I used datatables and everything was fine.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You would just install the Editor PHP libraries as they are normally, without using any Laravel functionality. They are effectively just installed alongside Laravel and they can exist independently.

    Allan

  • volnistii11volnistii11 Posts: 49Questions: 16Answers: 0

    Can you please tell me how I can correctly format the server script from your example into a method of my controller?

    example of your script

    <?php
     
    /*
     * Example PHP implementation used for the index.html example
     */
     
    // DataTables PHP library
    include( "../lib/DataTables.php" );
     
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
     
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'datatables_demo' )
        ->fields(
            Field::inst( 'first_name' )
                ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( 'A first name is required' ) 
                ) ),
            Field::inst( 'last_name' )
                ->validator( Validate::notEmpty( ValidateOptions::inst()
                    ->message( 'A last name is required' )  
                ) ),
            Field::inst( 'position' ),
            Field::inst( 'email' )
                ->validator( Validate::email( ValidateOptions::inst()
                    ->message( 'Please enter an e-mail address' )   
                ) ),
            Field::inst( 'office' ),
            Field::inst( 'extn' ),
            Field::inst( 'age' )
                ->validator( Validate::numeric() )
                ->setFormatter( Format::ifEmpty(null) ),
            Field::inst( 'salary' )
                ->validator( Validate::numeric() )
                ->setFormatter( Format::ifEmpty(null) ),
            Field::inst( 'start_date' )
                ->validator( Validate::dateFormat( 'Y-m-d' ) )
                ->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
                ->setFormatter( Format::dateFormatToSql('Y-m-d' ) )
        )
        ->debug(true)
        ->process( $_POST )
        ->json();
    

    My controller,method getAll()

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Models\Format;
    use Illuminate\Http\Request;
    
    
    class FormatController extends Controller
    {
        public function index()
        {
            return view('service.format.index');
        }
    
        public function getAll()
        {
            ......
        }
    
    
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Move line 22-52 from your first block of code into your getAll() method in the controller. I haven't used Laravel all that much myself, but it responds with JSON, so that should do the trick. You might need to make the $db global variable available - or otherwise create a new Database() class based on a Laravel PDO connection.

    Allan

  • volnistii11volnistii11 Posts: 49Questions: 16Answers: 0

    I tried.
    my routing

    Route::get('/service/formats/load', [FormatController::class, 'getAllJson'])->name('service.formats.getall');
    

    controller method

        <?php
        
        namespace App\Http\Controllers;
        
        use App\Models\Format;
        use DataTables\Editor;
        use DataTables\Editor\Field;
        use Illuminate\Http\Request;
        
        
        class FormatController extends Controller
        {
            public function index()
            {
                return view('service.format.index');
            }
        
            public function getAllJson()
            {
                global $db;
                Editor::inst($db, 'formats', 'id')
                    ->fields(
                        Field::inst('id'),
                        Field::inst('name')
                    )
                    ->process($_POST)
                    ->json();
            }
        
        
        }
    
    
    

    And got errors like this

    {
        "message": "Call to a member function transaction() on null",
        "exception": "Error",
        "file": "/application/vendor/datatables.net/editor-php/Editor.php",
        "line": 969,
        "trace": [
            {
                "file": "/application/vendor/datatables.net/editor-php/Editor.php",
                "line": 701,
                "function": "_process",
                "class": "DataTables\\Editor",
                "type": "->"
            },
            {
                "file": "/application/app/Http/Controllers/FormatController.php",
                "line": 26,
                "function": "process",
                "class": "DataTables\\Editor",
                "type": "->"
            },
            {
                "file": "/application/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
                "line": 54,
                "function": "getAllJson",
                "class": "App\\Http\\Controllers\\FormatController",
                "type": "->"
            },
            {
                "file": "/application/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
    .....
    .....
    .....
    
    ....
    thats not all
        ]
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    That suggests to me that $db is null. Did you require the DataTables.php file? It is what creates the $db variable in our examples. As I say though, you might need to modify that to use new \DataTables\Database(...) if you need to use a Laravel PDO object.

    Allan

Sign In or Register to comment.