0

I have an object in my database that I am trying to retrieve in the Model and parse through to display the text in the blade template. I am not quite sure how to get this to parse correctly. Thank you.

public function getStep1()
{
    $step1 = $this->details()->get();

    foreach($step1 as $step1)
    {
        $step1 = $step1->step_1;
        return $step1;
    }  
}

This gets me the following output that I don't know how to parse through. I have tried $step1[0], but that just gives me a [ nothing else.

[["$50,000-$100,000","More than $100,000"]]

My Blade template is simply this...

{{ $question->getStep1() }}

Thank you for your help.

Update:

When I do a var_dump, it says that my "steps" are stored as strings. Is that what's causing a problem? I still can't get within the [].

array (size=9)
      'id' => int 1
      'question_id' => int 55
      'step_1' => string '[["$50,000-$100,000","More than $100,000"]]' (length=43)
      'step_2' => string '[["Step2-option1","Step2-option2","Step2-option3"]]' (length=51)
      'step_3' => string '[["Step3-option2"]]' (length=19)
      'step_4' => null
      'step_5' => null
      'created_at' => string '2018-10-03 12:29:05' (length=19)
      'updated_at' => string '2018-10-03 12:29:05' (length=19)
1
  • $step1 as $step1 will not work since they are the same variable name. do $step1 as $step. Then inside that if $step is an array you'll have to do another foreach loop to get the individual array indexes. Commented Oct 9, 2018 at 12:24

2 Answers 2

1

Instead of returning double array you can just append results to a temporary array and then return that array, check the example code:

public function getStep1()
{
    $tempArr = [];
    $steps = $this->details()->get();

    foreach($steps as $step1)
    {
        $tempArr[] = $step1->step_1;
    }

    return $tempArr;
}

Your template code should now iterate over an array we have built right now, so a foreach is required. Like below:

@foreach($question->getStep1() as $step)
// do something with $step
@endforeach
Sign up to request clarification or add additional context in comments.

4 Comments

Update your question with code from your blade view
Are you trying to get a list of steps or just step 1? Im not sure from your question
From this update, I'm getting the error "Invalid argument supplied for foreach()". I'm just trying to get what's inside the "" ("$50,000-$100,000","More than $100,000"). Thank you.
Then you can access it with $step1[0][0] and $step1[0][1]
0
public function getStep1()
{
    $steps = $this->details()->get();
    $totalSteps = collect();
    foreach($steps as $step1)
    {
        foreach ($step1 as $step)
        {
             $toalSteps->push($step);
        }

    }
    return $totalSteps;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.