doufu1939 2019-05-03 03:44
浏览 601
已采纳

函数类似于__construct函数,但是对于LARAVEL上的特定函数

function like __construct function but for specific function.

my route

Route::resource('form00', 'Form00Controller');
Route::resource('form001', 'Form001Controller');
........ and more

my __contsruct in Form00Controller

public function __construct()
    {
        $this->validate(request(), [
            'projectName' => 
                array(
                    'required',
                    'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
                )
        ];
    }

my another Form00Controller function

    public function create()// this function and another not effected
    {
        return view('form00.create');
    }

    public function store(Request $request)//__construct just for this function
    {

        $requestData = $request->all();

        Form00::create($requestData);

        return redirect('form00')->with('flash_message', 'Form00 added!');
    }

i need validate just for function store and not change that function.

  • 写回答

1条回答 默认 最新

  • douhu7807 2019-05-03 04:56
    关注

    This is the perfect example for Custom Form Requests you can follow this tutorial

    Basically you'll end up with

    public function store(StoreFormFormRequest $request)//__construct just for this function
        {
    
            $requestData = $request->all();
    
            Form00::create($requestData);
    
            return redirect('form00')->with('flash_message', 'Form00 added!');
        }
    

    And you'll have a StoreFormFormRequest class into app/Http/Requests which will be something like

    class StoreFormFormRequest extends FormRequest {
    
        public function rules() {
            return [
                'projectName' => 'required|regex:/(^([a-zA-Z]+)(\d+)?$)/u'
            ]    
        }
    }
    
    

    Validation will be triggered automatically and, if it passes, the controller's code will be executed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python