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.');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路