Home > Net >  pass multiple params from function to funtion and access inputs -Laravel
pass multiple params from function to funtion and access inputs -Laravel

Time:12-24

$myCourseData['points'] = "1";                 
 $myCourseData['totalPoints'] = "2";

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

i wanted to pass points & totalPoints from a function to the function update() and access points in update() as $request->points;.How can i do that? Only points & totalPoints are passed from the above function , other params in the update() function input are getting from somewhere else.

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;
}          
    

    

CodePudding user response:

I think you wants to update only points & totalpoints from request.

then you can using request class's only function $input = Input::only('points', 'totalPoints');

Getting Only Some Of The Request Input

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

you can also use request helper instead of class

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

for more details about request instace check here Laravel official docs

CodePudding user response:

To answer it directly, you simply need to send the two Params as Params in the frontend request client.

If you're using a html form for instance, simply do

<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>

This would be submitted and captured by HTTP request library and passed as an object in the request variable like this:


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

Hopefully this answers your question.

CodePudding user response:

In your function update you have all validation data for update, you need just get object from DB. You model for updating

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

And read documentation

  • Related