dream_wu2015 2013-07-10 11:51
浏览 20
已采纳

Laravel 4中的Entrust权限更新未保存

I have a Laravel 4 installation, which I have added both Confide and Entrust packages.

I am trying to build an admin interface to manage users, roles, and permissions.
I have created a resourceful controller for permissions and both the add and destroy functions work fine, but the edit does not. The record is not updating the values in the database.

Here is the code for my controller:

<?php

class PermissionController extends BaseController {

     protected $permission;

     public function __construct(Permission $permission)
    {
        $this->permission = $permission;
    }

    ...

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $permission = $this->permission->find($id);

        if (is_null($permission))
        {
            return Redirect::route('permissions.index');
        }

        return View::make('permissions.edit', compact('permission'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Permission::$rules);

        if ($validation->passes())
        {
            $permission = $this->permission->find($id);
            // create name from display_name
            $input['name'] = $this->_machine_str($input['display_name']);

            try {
                $permission->update($input);
                $queries = DB::getQueryLog();
                $last_query = end($queries);
                print_r($queries);
                die();
            } catch (Exception $e) {
                var_dump($e->getTraceAsString());
                die();
            }


            return Redirect::route('permissions.show', $id);
        }

        return Redirect::route('permissions.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    ...
}

And the model:

<?php

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission
{
    /**
     *Soft delete enabled.
     * 
     * @var boolean 
     */
    protected $softDelete = true;

    protected $guarded = array('id');

    /**
     * Ardent validation rules
     *
     * @var array
     */
    public static $rules = array(
      'display_name' => 'required|between:4,32'
    );
}

The update works if I chnage the model to extend Eloquent, so I might be something to do with the EntrustPermission model.

I have also checked the database calls, which only show the selecting not any kind of update.

  • 写回答

1条回答 默认 最新

  • dpw63348 2013-07-10 14:51
    关注

    As the section in the docs which mentions the update() method is under the headline Saving A Model And Relationships, I'm assuming the update() method is best used for updating a model's relationships and is simply not documented clearly.

    If you instead take a "more manual" approach in updating the Permission model's attributes and using the save() method, you might have more success:

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Permission::$rules);
    
        if ($validation->passes())
        {
            $permission = $this->permission->find($id);
            // create name from display_name
            $input['name'] = $this->_machine_str($input['display_name']);
    
            try {
                // Change to update attributes "manually"
                $permission->name = $input['name'];
    
                // Update any other attributes as necessary
                $permission->any_other_attributes = $input['any_other_attributes']; // or  Input::get('any_other_attributes')
    
                // Finally, save (aka update) the Permission model's changes
                $permission->save();
    
                $queries = DB::getQueryLog();
                $last_query = end($queries);
                print_r($queries);
                die();
            } catch (Exception $e) {
                var_dump($e->getTraceAsString());
                die();
            }
    
    
            return Redirect::route('permissions.show', $id);
        }
    
        return Redirect::route('permissions.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化