在路由组中嵌套的CRUD上的Laravel Backpack 404(模型没有查询结果)

时间:2018-04-06 05:11:34

标签: laravel laravel-routing laravel-5.6 backpack-for-laravel

正如您在下面粘贴的代码中所看到的,我正在使用Route :: groups中的CRUD :: resources来嵌套我的cruds。这在第一级完美运行。但是,我有第二和第三级的问题。

我的crud索引完美无缺。创造也是如此。但是,编辑/更新和删除都有问题。我遇到404错误“模型[App \ Models \ TeamUserRole] 1”没有查询结果。

这是我的路线代码:

Route::group([
'prefix'     => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
'namespace'  => 'App\Http\Controllers\Admin'], function () {

CRUD::resource('user', 'UserCrudController');

CRUD::resource('tasks', 'TaskCrudController');

CRUD::resource('teams', 'TeamCrudController');
Route::group(['prefix' => 'teams/{team_id}'], function()
{
    CRUD::resource('tasks', 'TeamTaskCrudController');
    CRUD::resource('users', 'TeamUserCrudController');
    CRUD::resource('teams', 'TeamConnectionCrudController');

    Route::group(['prefix' => 'setup/'], function()
    {
        CRUD::resource('skills', 'TeamSkillCrudController');
        CRUD::resource('services', 'TeamServiceCrudController');
        CRUD::resource('categories', 'TeamCategoryCrudController');

        CRUD::resource('user/roles', 'TeamUserRoleCrudController');
        CRUD::resource('user/permissions', 'TeamUserPermissionCrudController');

        CRUD::resource('team/types', 'TeamTypeCrudController');
        CRUD::resource('team/features', 'TeamFeatureCrudController');

        CRUD::resource('team/connection/types', 'TeamConnectionTypeCrudController');
        CRUD::resource('team/connection/permissions', 'TeamConnectionPermissionCrudController');
    });     
}); });

这是我的crud控制器代码:

namespace App\Http\Controllers\Admin;

use App\Models\Team;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you 
need form validation
use App\Http\Requests\TeamUserRoleRequest as StoreRequest;
use App\Http\Requests\TeamUserRoleRequest as UpdateRequest;

class TeamUserRoleCrudController extends CrudController
{
public function setup()
{

    /*
    |--------------------------------------------------------------------------
    | BASIC CRUD INFORMATION
    |--------------------------------------------------------------------------
    */
    $this->crud->team_name = null;
    $this->crud->team_id = \Route::current()->parameter('team_id');
    if (isset($this->crud->team_id)) {
        $this->crud->team_name = Team::find($this->crud->team_id)->name;  
    }

    $this->crud->setModel('App\Models\TeamUserRole');
    $this->crud->setRoute(config('backpack.base.route_prefix') . '/teams/'.$this->crud->team_id.'/setup/user/roles');
    $this->crud->setEntityNameStrings('Team User Role', 'Team User Roles');

    // set a different route for the admin panel buttons
    $this->crud->addClause('Where', 'team_id', $this->crud->team_id);

    /*
    |--------------------------------------------------------------------------
    | BASIC CRUD INFORMATION
    |--------------------------------------------------------------------------
    */

    // $this->crud->setFromDb();

    // ------ CRUD FIELDS
    $this->crud->addField([ // Text
        'name' => 'name',
        'label' => "Name",
        'type' => 'text',

        // optional
        //'prefix' => '',
        //'suffix' => '',
        //'default'    => 'some value', // default value
        //'hint'       => 'Some hint text', // helpful text, show up after input
        //'attributes' => [
           //'placeholder' => 'Some text when empty',
           //'class' => 'form-control some-class'
         //], // extra HTML attributes and values your input might need
         //'wrapperAttributes' => [
           //'class' => 'form-group col-md-12'
         //], // extra HTML attributes for the field wrapper - mostly for resizing fields 
         //'readonly'=>'readonly',
    ], 'update/create/both');

    $this->crud->addField([
        'label'     => 'Permissions',
        'type'      => 'checklist',
        'name'      => 'permissions',
        'entity'    => 'permissions',
        'attribute' => 'name',
        'model'     => 'App\Models\TeamUserPermission',
        'pivot'     => true,
    ]);

    // $this->crud->addField($options, 'update/create/both');
    // $this->crud->addFields($array_of_arrays, 'update/create/both');
    // $this->crud->removeField('name', 'update/create/both');
    // $this->crud->removeFields($array_of_names, 'update/create/both');

    // ------ CRUD COLUMNS
    $this->crud->addColumn([
       'name' => 'name', // The db column name
       'label' => "Role", // Table column heading
       // 'prefix' => "Name: ",
       // 'suffix' => "(user)",
       // 'limit' => 120, // character limit; default is 80;
    ]);

    $this->crud->addColumn([ // n-n relationship (with pivot table)
        'label'     => 'Permissions',
        'type'      => 'select_multiple',
        'name'      => 'permissions',
        'entity'    => 'permissions',
        'attribute' => 'name',
        'model'     => 'App\Models\TeamUserPermission',
        'pivot'     => true,
    ]);
}

public function store(StoreRequest $request)
{
    /* Set Team ID */
    $request->request->set('team_id',$this->crud->team_id);

    // your additional operations before save here
    $redirect_location = parent::storeCrud($request);
    // your additional operations after save here
    // use $this->data['entry'] or $this->crud->entry
    return $redirect_location;
}

public function update(UpdateRequest $request)
{
    // your additional operations before save here
    $redirect_location = parent::updateCrud($request);
    // your additional operations after save here
    // use $this->data['entry'] or $this->crud->entry
    return $redirect_location;
}
}

1 个答案:

答案 0 :(得分:0)

好的......我可能已经解决了这个......

我将此添加到我的crud顶部以从路由参数中检索并存储用户ID:$ this-> crud-> user_id = \ Route :: current() - >参数(&#39 ;用户&#39);

然后,我从CrudController类中复制了edit($ id)函数,并用$ this-> crud-> user_id覆盖了$ id变量。

这似乎有效。但是,想知道是否应该修改CrudController以便能够处理嵌套参数。