【问题标题】:pass multiple params from function to funtion and access inputs -Laravel将多个参数从函数传递到函数并访问输入-Laravel
【发布时间】:2022-01-25 00:37:32
【问题描述】:
$myCourseData['points'] = "1";                 
 $myCourseData['totalPoints'] = "2";

$this->update($myCourseData,$myCourseId);

我想将 pointstotalPoints 从函数传递到函数 update() 并在 update() 中以 $request->points; 访问 points。我该怎么做?只有points & totalPoints 是从上面的函数传递过来的,update() 函数输入中的其他参数是从其他地方获取的。

function update($request,$id){

 $validator = Validator::make(
            $request->all(),
            [
                'course_id'       => 'nullable|integer',  
                'exam_numbers'    => 'nullable|integer',
                'points'          => 'nullable|integer', 
                'subscription' => 'nullable|boolean',
                'totalPoints'=>'nullable|integer'
            ]
        );     

$points =  $request->points;
}          
    

    

【问题讨论】:

  • 你的意思是你想将数据作为请求对象传递?

标签: php laravel validation laravel-8 validationrules


【解决方案1】:

我认为您只想根据请求更新积分和总积分。

那么你可以使用请求类的唯一函数$input = Input::only('points', 'totalPoints');

仅获取部分请求输入

$input = Input::only('points', 'totalPoints');
// OR
$input = Input::except('course_id', 'exam_numbers', 'subscription');

你也可以使用请求助手代替类

$input = $request->only('points', 'totalPoints')
// OR
$input = $request->except('course_id', 'exam_numbers', 'subscription');

有关请求实例的更多详细信息,请点击此处Laravel official docs

【讨论】:

    【解决方案2】:

    直接回答,只需要在前端请求客户端将两个Param作为Params发送即可。

    例如,如果您使用的是 html 表单,只需这样做

    <form action="route-pointing-to-update-function" method="post"> 
    <!-- Make sure you're sending a csrf token with the request -->
    <input type="number" name='points' value="1">
    <input type="number" name='totalPoints' value="3">
    
    Submit
    </form>
    
    

    这将被 HTTP 请求库提交和捕获,并作为请求变量中的对象传递,如下所示:

    
    public function update (Request $request, $id){
     // Now you can access it like that here.
    $request->points;
    }
    

    希望这能回答你的问题。

    【讨论】:

      【解决方案3】:

      在您的函数update 中,您拥有所有要更新的验证数据,您只需从数据库中获取对象。你model更新了

      $modelObj = SomeModel::find($id);
      $modelObj->update([
         'points' => $request->points,
         'totalPoints' => $request->totalPoints
      ]);
      

      阅读documentation

      【讨论】:

        【解决方案4】:

        您可以将这些输入合并到请求中:

        $request->merge([
            'points' => 1,
            'totalPoints' => 2,
        ]);
        
        $this->update($request, $myCourseId);
        

        【讨论】:

          【解决方案5】:

          我认为您需要在请求类对象中添加积分和总积分,然后发送到更新功能。在这里,我尝试只使用一个控制器来实现。

          <?php
          use Illuminate\Http\Request;
          use App\Http\Controllers\Controller;
          
          
          class DashboardController extends Controller
          {
              public function abc(){
                  $myCourseData = new Request(["points" => "1", "totalPoints" => "2"]);
                  $myCourseId = 5;
          
                  $this->update($myCourseData,$myCourseId);
              }
          
              public function update(Request $request, $id){
                  $points =  $request->points;
                  dd($points);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2022-01-27
            • 2012-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-18
            • 2021-04-13
            • 1970-01-01
            • 2016-04-02
            相关资源
            最近更新 更多