Pass a variable from one function to another Laravel PHP?

Asked

Viewed 1,426 times

0

Hello someone could help me? I’m having trouble passing function parameters to another. I don’t know if I’m doing it right, I’m new to PHP.

public function verificarCondutor(Request $request)
{
    //dados do formulário
    $cartaoRFID = $request->cartaoRFID;
    $cancelaTipo = $request->cancela;

    $this->indexJson($cartaoRFID);

}


 public function indexJson($cartaoRFID)
{
    $cartaoRFID = $cartaoRFID;
    return $cartaoRFID;
}
  • 1

    What you did is right (if both functions belong to the same class), but as indexJson is of no use in the example it is difficult to understand what you are trying to do.

  • give a var_dump in $this->indexJson($cartaoRFID) and see if you got the result you wanted

  • It’s all right, only you do absolutely nothing with the value, if you give a dd() or var_dump() in the return or inside the function, you will be able to test the values.

  • Your code has conceptual errors, your code has a no return method and in the other you make the same variable receive it even it is not necessary and to use one method in the other is reserved $this and the method name. What you want to do? can improve your question by editing

3 answers

2


What was missing was a 'Return' in the first method.

return $this->indexJson($cartaoRFID);

Only the second function does not make any changes in the variable.

If you want to return a json there is a answer... Try the first method.

return response()->json($request->all());

Will return all the request.

If you want to return only the variable in json format do.

return response()->json(['cartao' => $cartaoRFID]);

0

Hello, Beatriz! You have in this code two methods, which must be in a class. From this pre-presumed you can pass parameters to another method using the $this, which refers to the instantiated object of the class, gets its return and saves in a variable to use it later.

That way, you can do the following:

<?php

//...
public function getIndexJson($cartaoRFID)
{
    // Faz toda a lógica que precisa dentro dessa função.
    $indexJson = [];

    // Então retorna o resultado
    return $indexJson;
}

public function verificarCondutor(Request $request)
{
    // Salva os dados enviados pelo formulário em variáveis
    $cartaoRFID = $request->cartaoRFID;
    $cancelaTipo = $request->cancela;

    // Passar o valor de $cartaoRFID para o método getIndexJson() e salva o seu retorno na variável $indexJson.
    $indexJson = $this->getIndexJson($cartaoRFID);

    //... continua com a lógica de verificar condutor
}

I hope it helped. :)

0

PHP basically supports two types of parameter passages for functions: pass by value and pass by reference. Passing by value does not change the value of the variable outside the function after its internal use. Example:

<?php
function add_some_extra($string)
{
    $string .= ' e alguma coisa mais.';
}
$str = 'Isto é uma string,';
add_some_extra($str);
echo $str;    // imprime 'Isto é uma string,'
?>

However, if the function parameter is passed by reference through character & placed immediately before the parameter, the value of the variable will be changed both inside and outside the function. Example:

<?php
function add_some_extra(&$string)
{
    $string .= ' e alguma coisa mais.';
}
$str = 'Isto é uma string,';
add_some_extra($str);
echo $str;    // imprime 'Isto é uma string, e alguma coisa mais.'
?>

Also, if you are going to use object orientation, you could use the attributes of a class to modify the value of the desired variable within one method, and be able to access it in another method. Based on your code it would look like this:

...
class Condutor{
 public $cartaoRFID;
 public function verificarCondutor(Request $request)
 {
    //dados do formulário
    $this->cartaoRFID = $request->cartaoRFID;
    $cancelaTipo = $request->cancela;

    $this->indexJson();

 }

 public function indexJson()
 {
    return $this->cartaoRFID;
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.