0

At

$position = $request->f;
        $socio = Socio::where('user_id', '=', Auth::user()->id)->first();
        $array = $socio->socios;

The echo out of $array is:

0: {nome: "JANNES MARCON DELAZERI", cpf: "698.969.869-69", rg: "45.455.357-4", orgaoExp: "1231"}
1: {nome: "JOVILDE INES DELAZERI", cpf: "123.423.452-34", rg: "67.856.785-6", orgaoExp: null}
2: {nome: "POLLYANA DALL CORTIVO BALESTRERI", cpf: "098.787.698-58", rg: "65.432.432-5", orgaoExp: null}
3: {nome: "CARLOS ALBERTO DELAZERI", cpf: "123.235.265-32", rg: "76.356.424-6", orgaoExp: null}

Now, I want to grab data from index 2, so I did:

$fiador = in_array($position, $array);
        return $fiador;

But, it returns an error like:

in_array() expects parameter 2 to be array, string given

What I am doing wrong? What did I miss?

2 Answers 2

1

$array variable is json. You need to convert to array.

    $position = $request->f;
    $socio = Socio::where('user_id', '=', Auth::user()->id)->first();
    $array = $socio->socios;

    if($array) {
        $array = json_decode($array, 1);
        $fiador = in_array($position, $array);
        return response()->json(['status' => $fiador]);
    }
    return response()->json(['status' => false], 404);

Sign up to request clarification or add additional context in comments.

4 Comments

How do You know it is JSON? They look very similar... Now Laravel give me this error: ``` message: "The Response content must be a string or object implementing __toString(), "boolean" given." ```
Your error is solved. I do not know this return is a response because you did not give detailed information. I updated my answer. Can you try again?
Now the result is {status: false}. What is like 'if' statement wasn't there, right?
Yes. $socio->socios return empty
0

Well, the help from @alpmkeskekoglu made me think and I found the answer:

public function pegaFiador(Request $request)
    {
        $position = $request->f;
        $socio = Socio::where('user_id', '=', Auth::user()->id)->first();
        $json = $socio->socios;
        $array = json_decode($json, true);
        return $array[$position];
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.