laravel - access variable outside loop - laravel

my code:
$atts->each(function($row){
if($row->key == 30){
$flag = $row->value;
echo $flag;
}
});
The value of $flag will be printed to the site. However, when I try to use the variable $flag outside the loop the variable is unknown.
Can someone tell me what I am missing or what has to be done to have access to that variable?
Thanks in advance!
Regards,
Andreas

You would need to define that variable in the current scope and import that variable into your callback's scope with use.
$flag = null;
//Access flag by reference
$atts->each(function($row) use (&$flag) {
...
});
Since we're using Laravel Collections, I suggest you not use each() for this.
if ($row = $atts->where('key', 30)->first()) {
$flag = $row->value;
}
This assumes there is only one row with a key of 30 though.

Related

Laravel check property exist

How I can check existing property $this->team->playerAssignment->player in more rational way? Now I check it like this:
if ($this->team)
if (($this->team->playerAssignment))
if (($this->team->playerAssignment->player))
Try isset php function.
isset — Determine if a variable is set and is not NULL
if(isset($this->team->playerAssignment->player)){
}
The following has always works best for me:
if(isset(Auth::user()->client->id)){
$clientId = Auth::user()->client->id;
}
else{
dump('Nothing there...');
}
The best way is
if (
isset($this->team)
&& isset($this->team->playerAssignment)
&& isset($this->team->playerAssignment->player)
){
// your code here...
}
Because PHP will stops if the first is to false, if first object exists, it continue to second, and third condition...
Why not use only && $this->team->playerAssignment->player ?! because if player has 0 for value it will be understood as a false but variable exists !
You can easily check by null coalescing operator
if ($this->team->playerAssignment->player ?? null)

Get a value and than use that value to compare

I am facing a problem with a PowerShell variable.
My scenario is,
Inside a function, I declare a variable $a, than in a switch, I get a value and store this to variable $a.
Now in another switch in that function, I want to compare $a. But there $a returns null.
Sample code is given below:
function fun
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
$param
)
$Get_OldData = " " #declare variable
switch ($param){
'param_001' {
$Get_OldData = "test data returned"
}
Default {
$Get_OldData = "test data returned"
}
}
switch ($param){
'param_001' {
$New_Data = "New Data"
#problem is here, can not compare $Get-OldData returns null here
#though data is assigned
if ( $New_Data -eq $Get_OldData){
#logic goes here
}
}
Default {
$New_Data = "New Data"
}
}
}
What is the solution of this problem?
You have multiple issues with your code.
The main issue probably is that you are using $param within your switch which has not been set. Same applies to $Fetch. Another Issue is that your $New-Data variable contains a hypen which you either should replace with an underscore or surround with curly brackets like ${New-Data}.
Also, // does not introduce a comment, you have to use a hash #.

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

Format Output of Placeholder

I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]

PHP: How to transport vars from php cmd (executing from shell) to PHP GET and POST Array?

i have some scripts that need GET and POST values to start, and i wanna test them over shell.
Is there some way to pass the values to that arrays to avoid using getenv() function?
Thanks in advance!
if your main goal is just to test from the command line, I would use the wget command and just call your script with the query string (for GET) and pass post data using the --post-data=string parameter of wget (for POST).
If your goal is to not use a webserver at all for testing for some reason, I'd recommend using a wrapper and encapsulating your access to GET and POST data so that you can test it either way.
What you need is a wrapper script that sets the relevant globals and environment variables and then calls your script.
Try something like this:
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])){ // make sure we're running in CLI
$args = $argv; // copy the argv array, we want to keep the original for (possible) future uses
array_shift($args); // the $argv[0] is the filename, we don't need it
for($i = 0;$i < ($argc - 1);$i++){
list($key, $value) = explode('=', $args[$i]);
$_REQUEST[$key] = $value;
}
}
Of course, more features can be added by using getopts (like, --get abc=def ghi=jkl --post name=test passwd=test --cookie ilike=cookie) but that's up to you.
if(php_sapi_name() == 'cli')
{
associateGetPost();
}
function associateGetPost()
{
$_GET = $_POST = array(); //Reset
foreach($args as $id => $value)
{
if(substr($value,0,5) == '--get')
{
$_GET = parse_str(substr($value,5,-1))
}elseif(substr($value,0,6) == '--post')
{
$_GET = parse_str(substr($value,6,-1))
}
}
}
Something along those lines.
Take a look at: $_REQUEST
In command line you call as
php test.php something1 something2 something3
and your test.php is
<?php
print_r($argv);
?>
and output is
Array
(
[0] => test.php
[1] => something1
[2] => something2
[3] => something3
)

Resources