【问题标题】:Laravel update codeLaravel 更新代码
【发布时间】:2025-11-26 05:20:02
【问题描述】:

我正在尝试根据某些条件更新用户表的值。在 laravel 中,编辑页面中的表单操作 url 是:

http://127.0.0.1:8005/panel/User/edit?update=345

但我无法找到请求的去向,但我的值正在更新。没有更新方法。我的 UserController 扩展了 CrudController(ServerFireTeam):

class UserController extends CrudController{

    public function all($entity){
        parent::all($entity); 

        /** Simple code of  filter and grid part , List of all fields here : http://laravelpanel.com/docs/master/crud-fields
         */

        $this->filter = \DataFilter::source(User::with('timezone'));
        $this->filter->add('email', 'Email', 'text');
        $this->filter->add('business_name', 'Business Name', 'text');
        $this->filter->add('first_name', 'First Name', 'text');
        $this->filter->add('last_name', 'Last Name', 'text');
        $this->filter->add('plan_name', 'Plane Name', 'text');
        $this->filter->submit('search');
        $this->filter->reset('reset');
        $this->filter->build();

        $this->grid = \DataGrid::source($this->filter);
        $this->grid->add('id', 'ID');
        $this->grid->add('email', 'Email');
        $this->grid->add('timezone.name','Timezone');
        $this->grid->add('business_name', 'Business Name');
        $this->grid->add('first_name', 'First Name');
        $this->grid->add('last_name', 'Last Name');
        $this->grid->add('dob', 'DOB');
        $this->grid->add('sign_in_count', 'Sign In Count');
        $this->grid->add('current_sign_in_at', 'Current Sign In At');
        $this->grid->add('current_sign_in_ip', 'Current Sign In IP');
        $this->grid->add('last_sign_in_at', 'Last Sign In At');
        $this->grid->add('last_sign_in_ip', 'Last Sign In IP');
        $this->grid->add('subscription_id', 'Subscription ID');
        $this->grid->add('status', 'Status');

        $this->addStylesToGrid();



        return $this->returnView();
    }

    public function  edit($entity){



        parent::edit($entity);

        /* Simple code of  edit part , List of all fields here : http://laravelpanel.com/docs/master/crud-fields
        */
        $this->edit = \DataEdit::source(new User());

       /*echo '<pre>';
        print_r($val);

       echo '</pre>';
*/
        $this->edit->label('Edit User');

        $this->edit->add('email', 'Email', 'text')->rule('required');
        $this->edit->add('timezone_id','Timezone','select')->options(Timezone::pluck("name", "id")->all())->rule('required');
        $this->edit->add('business_name', 'Business Name', 'text')->rule('required');
        $this->edit->add('first_name', 'First Name', 'text');
        $this->edit->add('last_name', 'Last Name', 'text');
        $this->edit->add('subscription_id', 'Subscription ID', 'text');
        $this->edit->add('status', 'Status', 'text');
        //$this->edit->add('plan_name', 'Plan Name', 'text');
        $plan=$this->edit->add('plan_name','Plan Name','select')->options(plan::pluck("name", "id")->all())->rule('required');

        /*echo '<pre>';
        print_r($plan);

        echo '</pre>';
*/
        return $this->returnEditView();
    }    
}

基础控制器:

class CrudController extends Controller
{
    const ID_COLUMN = 'id';

    public    $grid;
    public    $entity;
    public    $set;
    public    $edit;
    public    $filter;
    protected $lang;
    public    $helper_message;

    public function __construct(\Lang $lang)
    {
       // $this->entity = $params['entity'];
        $route = \App::make('route');
        $this->lang = $lang;
        $this->route = $route;
        if($route = $route::current())
        {
            $routeParamters = $route->parameters();
            if(isset($routeParamters['entity']))
                $this->setEntity($routeParamters['entity']);
        }
    }

    /**
    * @param string $entity name of the entity
    */
    public function all($entity)
    {
        //$this->addStylesToGrid();
    }

    /**
    * @param string $entity name of the entity
    */
    public function edit($entity)
    {

    }

    public function getEntity() {
        return $this->entity;
    }

    public function setEntity($entity) {
        $this->entity = $entity;
    }

    public function getEntityModel() {

        $entity = $this->getEntity();

        $appHelper = new libs\AppHelper;

        $modelClass = $appHelper->getModel($entity);

        return new $modelClass;
    }

    public function addStylesToGrid($orderByColumn = self::ID_COLUMN, $paginateCount = 10)
    {
        $this->grid->edit('edit', trans('panel::fields.edit'), 'show|modify|delete');


        if ($orderByColumn === self::ID_COLUMN) {
            $orderByColumn = $this->getEntityModel()->getKeyName();
        }

        $this->grid->orderBy($orderByColumn, 'desc');
        $this->grid->paginate($paginateCount);
    }

    public function addHelperMessage($message)
    {
        $this->helper_message = $message;
    }

    /**
     * Check whether a link is defined for the given URL / model name
     * @param $url
     * @throws \Exception
     */
    private function validateEntity($url) {
        if (!\Links::all()->pluck('url')->contains($url)) {
            throw new \Exception('This url is not set yet!');
        }
    }

    public function returnView()
    {
        $this->validateEntity($this->entity);
        return \View::make('panelViews::all', array(
         'grid'           => $this->grid,
         'filter'         => $this->filter,
         'title'          => $this->entity ,
         'current_entity' => $this->entity,
         'import_message' => (\Session::has('import_message')) ? \Session::get('import_message') : ''
        ));
    }

    public function returnEditView()
    {

        $this->validateEntity($this->entity);
        return \View::make('panelViews::edit', array('title'         => $this->entity,
                                'edit'       => $this->edit,
                        'helper_message' => $this->helper_message));


    }

    public function finalizeFilter() {
        $lang = \App::make('lang');
        $this->filter->submit($this->lang->get('panel::fields.search'));
        $this->filter->reset($this->lang->get('panel::fields.reset'));
    }
}

【问题讨论】:

  • 发布基本控制器。
  • 添加了基本控制器。
  • @LeoinstanceofKelmendi 请检查基本控制器
  • hmm 一些意大利面条代码,什么版本的 laravel ?
  • 版本是- 5.4.36

标签: php laravel


【解决方案1】:

路由参数存储在基本控制器上:

  if($route = $route::current())
        {
            $routeParamters = $route->parameters();
            if(isset($routeParamters['entity']))
                $this->setEntity($routeParamters['entity']);
        }

首先检查是否有任何参数,如果有,然后使用 setter 设置属性 public $entity;

public function setEntity($entity) {
    $this->entity = $entity;
}

【讨论】:

  • 如何生成更新方法并在其中写入自己的更改?
  • 正如我所说,这是一些 sphagetti 代码,只需定义一个更新方法并使用包含路由参数的 $entity 属性。我会完全重做那些控制器太臃肿了。