Thursday, March 2, 2017

filter with select2_ajax in laravel backpack not work

hello im new at laravel backpack. Here i want to create filter in my backpack but ajax always return 'loading error'.. and here my code

route.php

Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {

    Route::get('ajax', 'Admin\RelationCrudController@categoryOptions');
    CRUD::resource('relation', 'Admin\RelationCrudController');
};

RelationCrudController.php

    <?php
namespace App\Http\Controllers\Admin;

use App\Http\Requests\RelationRequest as StoreRequest;
use App\Http\Requests\RelationRequest as UpdateRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use App\Komsel;

class RelationCrudController extends CrudController
{

    public function setup()
    {
        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
         */
        $this->crud->setModel("App\Relation");
        $this->crud->setRoute("admin/relation");
        $this->crud->setEntityNameStrings('relation', 'relations');

        /*
        |--------------------------------------------------------------------------
        | COLUMNS AND FIELDS
        |--------------------------------------------------------------------------
         */

        // ------ CRUD COLUMNS
        $this->crud->addColumn([
            'label'     => "People",
            'type'      => 'select',
            'name'      => 'people_id',
            'entity'    => 'people',
            'attribute' => 'name_people',
            'model'     => "App\People",
        ]);
        $this->crud->addColumn([
            'label'     => "Komsel",
            'type'      => 'select',
            'name'      => 'komsel_id',
            'entity'    => 'komsel',
            'attribute' => 'name_komsel',
            'model'     => "App\Komsel",
        ]);

        $this->crud->addColumn([
            'label'     => "Status",
            'type'      => 'select',
            'name'      => 'status_id',
            'entity'    => 'status',
            'attribute' => 'status',
            'model'     => "App\Komsel",
        ]);

        $this->crud->addColumn([
            'name'  => 'date_join',
            'label' => "Join Date ke Komsel",
        ]);

        // ------ CRUD FIELDS

        $this->crud->addField([ // SELECT
            'label'     => "People",
            'type'      => 'select2',
            'name'      => 'people_id',
            'entity'    => 'people',
            'attribute' => 'name_people',
            'model'     => "App\People",
        ]);

        $this->crud->addField([ // SELECT
            'label'     => "Komsel",
            'type'      => 'select2',
            'name'      => 'komsel_id',
            'entity'    => 'komsel',
            'attribute' => 'name_komsel',
            'model'     => "App\Komsel",
        ]);

        $this->crud->addField([ // SELECT
            'label'     => "Status",
            'type'      => 'select2',
            'name'      => 'status_id',
            'entity'    => 'status',
            'attribute' => 'status',
            'model'     => "App\Status",
        ]);

        $this->crud->addField([ // TEXT
            'name'  => 'date_join',
            'label' => 'Join Date ke Komsel',
            'type'  => 'date',
            'value' => date('Y-m-d'),
        ], 'create');

        $this->crud->addField([ // TEXT
            'name'  => 'date_join',
            'label' => 'Join Date',
            'type'  => 'date',
        ], 'update');

        $this->crud->addFilter([ // select2_ajax filter
            'name'        => 'komsel_id',
            'type'        => 'select2_ajax',
            'label'       => 'Komsel',
            'placeholder' => 'Pick a Komsel',
        ],
            url('ajax'), // the ajax route
            function ($value) {
                // if the filter is active
                $this->crud->addClause('where', 'komsel_id', $value);
            });
    }

    public function store(StoreRequest $request)
    {
        return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
        return parent::updateCrud();
    }

    public function categoryOptions()
    {
        $term    = $this->request->input('term');
        $options = Komsel::where('name_komsel', 'like', '%' . $term . '%')->get();
        return $options->pluck('name_komsel', 'komsel_id');
    }

}

if i run http://localhost:8000/admin/ajax in my pages, this can return array contain of my data

{"1":"komsel 1","2":"komsel 2","3":"komsel 3"}

but still when i type 'komsel 1/komsel 2' in the filter field it always return loading error.. can u help me guys? thanks




via san

Advertisement