PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] pass params from function to funtion and access inputs -Laravel

 December 30, 2021     arrays, laravel, laravel-8, php, validation     No comments   

Issue

The below code shows the error TypeError: Argument 1 passed to App\Http\Controllers\StudentLearningController::update() must be an instance of Illuminate\Http\Request, array given

use Illuminate\Http\Request;
class StudentLearningController extends Controller{

 public function abc(){
    $myCourseData['points'] = "1";                 
     $myCourseData['totalPoints'] = "2";
    
    $this->update($myCourseData,$myCourseId);
  }
}

i wanted to pass 2 parameters: points & totalPoints from function abc() 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 updated from some other function.

 use Illuminate\Http\Request;
trait MyCourseTrait{
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;
 }    
}    

I know, the above given error is produced becoz am passing an array to the input of update() function .But i can't simply do it as

$points="1";
$totlaPoints="2";
$this->update($points,$totlaPoints,$myCourseId);

becoz the update() function in MyCourseTrait can't have more than 2 params : $request & $id , as this function is used in some other cases in my project , where only 2 params are provided. This is the actual issue am facing.


Solution

You are passing an array to the first argument of the update function which I guess should be an instance of Request

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


    class DashboardController extends Controller
    {
        public function abc(){
            $myCourseData = new Request(["points" => "1"]);
            $myCourseId = 5;
    
            $this->update($myCourseData,$myCourseId);
        }
    
        public function update(Request $request, $id){
            $points =  $request->points;
            dd($points);
        }
    }


Answered By - Iamshriyogi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing