Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enableAjaxTable gives Undefined variable: entry #207

Closed
mpixelz opened this issue Oct 27, 2016 · 1 comment
Closed

enableAjaxTable gives Undefined variable: entry #207

mpixelz opened this issue Oct 27, 2016 · 1 comment

Comments

@mpixelz
Copy link

mpixelz commented Oct 27, 2016

Hi,
i have backpack crud controller.. im using one controller to divide the user list depending on their status..
here is my controller:

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

use Backpack\CRUD\app\Http\Controllers\CrudController;
use validator;
use Illuminate\Http\Request;
use Spatie\Permission\Traits\HasRoles;
use App\User;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\StudentListsRequest as StoreRequest;
use App\Http\Requests\StudentListsRequest as UpdateRequest;

class StudentListsCrudController extends CrudController {
use HasRoles;
    public function __construct() {
        parent::__construct();

        $this->crud->setModel("App\Models\StudentLists");
        $this->crud->setRoute("admin/student-list");
        $this->crud->setEntityNameStrings('New Student', 'Student List');
        // $this->crud->enableAjaxTable();
        $this->crud->enableExportButtons();

        $this->crud->setFromDb();
        $this->crud->addField([
                            'name' => 'password_confirmation',
                            'label' => 'Password Confirmation',
                            'type' => 'password',
                            ]);
    $this->crud->setModel("App\User");
        $this->crud->addColumn([ // n-n relationship (with pivot table)
       'label' => "Roles", // Table column heading
       'type' => "select_multiple",
       'name' => 'roles', // the method that defines the relationship in your Model
       'entity' => 'roles', // the method that defines the relationship in your Model
       'attribute' => "name", // foreign key attribute that is shown to user
       'model' => "Backpack\PermissionManager\app\Models\Roles", // foreign key model
        ]);
        $this->crud->removeColumns(['password', 'roles', 'assign_user']); // remove an array of columns from the stack
        $this->crud->addClause('whereHas', 'roles', function($query) {
            $query->where('name', 'Student');
        });
    }

    public function index()
    {
    $this->crud->hasAccessOrFail('list');
    $this->data['crud'] = $this->crud;
    $this->data['title'] = ucfirst($this->crud->entity_name_plural);
    $userallocation = User::all()->filter(function (User $user) { return $user->hasPermissionTo('user_allocation'); });
    // get all entries if AJAX is not enabled
    if (! $this->data['crud']->ajaxTable()) {
        $this->data['entries'] = $this->data['crud']->getEntries();
    }
    return view('StudentLists', $this->data)
    ->with('userallocation', $userallocation);
    }

    public function prospective()
    {
    $this->crud->setEntityNameStrings('New Student', 'Student List - Prospective');
    $this->crud->hasAccessOrFail('list');

    $this->data['crud'] = $this->crud;
    $this->data['title'] = ucfirst($this->crud->entity_name_plural);
    $userallocation = User::all()->filter(function (User $user) { return $user->hasPermissionTo('user_allocation'); });
    // // get all entries if AJAX is not enabled
    if (! $this->data['crud']->ajaxTable()) {
        $this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', null);
    }
    return view('StudentLists', $this->data)
    ->with('userallocation', $userallocation)
    ;
    }

    public function leads()
    {
    $this->crud->hasAccessOrFail('list');
    $this->data['crud'] = $this->crud;
    $this->data['title'] = ucfirst($this->crud->entity_name_plural);
    $this->crud->removeButton('create');


    $userallocation = User::all()->filter(function (User $user) { return $user->hasPermissionTo('user_allocation'); });

    $this->crud->addColumn([
   'name' => 'assign_user', // The db column name
   'label' => 'Assigned User', // Table column heading
    ]);
    $this->crud->addColumn([
   // run a function on the CRUD model and show its return value
   'label' => "Assign User", // Table column heading
   'type' => "model_function",
   'function_name' => 'assignauser', // the method in your Model
    ]);

    // get all entries if AJAX is not enabled
    if (! $this->data['crud']->ajaxTable()) {
        $this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', 1);
    }
    return view('StudentLists', $this->data)
    ->with('userallocation', $userallocation)
    ;
    }

    public function assigneduser(Request $request)
    {
        $user_id = $request->id;
        $user = User::firstOrNew(['id' => $user_id]);
        $formdata = $request->all();
        $user->update($formdata);
        return \Redirect::back();
    }

    public function create()
    {
        $this->crud->hasAccessOrFail('create');
        // prepare the fields you need to show
        $this->data['crud'] = $this->crud;
        $this->data['fields'] = $this->crud->getCreateFields();
        $this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name;

        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
        return view('crud::create', $this->data);
    }
    public function store(StoreRequest $request)
    {
        // insert item in the db
        if ($request->input('password')) {
            $item = $this->crud->create(\Request::except(['redirect_after_save']))->assignRole('Student');
            // now bcrypt the password
            $item->password = bcrypt($request->input('password'));
            $item->save();
        } else {
            $item = $this->crud->create(\Request::except(['redirect_after_save', 'password']));
        }

        // show a success message
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
        // redirect the user where he chose to be redirected
        switch (\Request::input('redirect_after_save')) {
            case 'current_item_edit':
                return \Redirect::to($this->crud->route.'/'.$item->id.'/edit');
            default:
                return \Redirect::to(\Request::input('redirect_after_save'));
        }
        //return parent::storeCrud();
    }

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

using the above controller (not using ajax table) it works but only if there are any records in that table..
and if i enable the ajaxtable it gives me:
Undefined variable: entry

even if i disable the ajaxtable and if there are no records meeting the criteria i still get the same error..
can anyone point out what am i doing wrong here? :(

@mpixelz
Copy link
Author

mpixelz commented Oct 27, 2016

found the issue... it was my own jquery where i was getting the id of the user to be edited in the modal window using: {{$entry->getKey()}}

@mpixelz mpixelz closed this as completed Oct 27, 2016
This was referenced Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant