Monday, February 4, 2019

phpMyPassion

Laravel Advanced how to pass variable into nested where function?


Whenever you want to use a variable inside the Closure scope, you need to use the "use" keyword to pass the variable into the Closure:


foreach ($user->locations as $location) {  
        $r = TableName::where('id', '<>', $this->id)    
            ->where(function ($q) use ($code) { // SEE HERE
                      $q->where('name', $code)        
                      ->orWhere('alias', $code); })
            ->get();
}



if you have an array variable like - $requestParam['name'] .

you have to define it before the query statement -


$name = $requestParam['name'];
$lastRecordResult = YrModel::where('type', self::table)
  ->where(function ($q) use ($name) {    
                $q->where('name', $name)      
                ->orWhere('alias', $name);  })  
  ->where('t_id', $requestParam['t_id'])  
  ->where('v_id', $requestParam['v_id'])  
  ->count();



About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a hug of information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :