1

I'm working with a Laravel for a school project and I'm haing osme difficulties solving this problem. The code is exactly what I got from another script and I already double checked the code.

Function Code:

$modules = Module::all();
$id = Auth::user()->id;

$array = array();

foreach($modules as $module) {

  $questions = Question::where('m_id', $module['id'])->count();
  $answers = Answer::where('m_id', $module['id'])->where('user_id', $id)->where('correct', 1)->count();

  $percentage = ($answers / $questions) * 100;

  array_push($array, array(
    'name' => $module->name,
    'description' => $module->description,
    'precentage' => $percentage
  ));
}

print_r($array);

return View::make('hello')->with('modules', $array);

In the print_r($array) function I'm getting exactly everything that I want to get in the view. But the problem is that I don't get anything with me, I'm checking it with {{ Session::has('modules') }} and it returns false (not set).

Any solution to this?

3
  • Where is this function located...?? Commented Feb 10, 2015 at 17:23
  • Try this: print_r(Input::get('modules')); Commented Feb 10, 2015 at 17:27
  • this function is located in HomeController, but don't really see why that makes a diffrence. Commented Feb 10, 2015 at 17:29

1 Answer 1

2

Why are you checking for the modules key in Session? You're not setting it in session, you're returning that data with the view. You should be able to do something like this in your view given the code above:

@foreach($modules as $module)
    {{ $module['name'] }}
@endforeach

In order for Session::has('modules') to return true, you would need to do something like this before you return the view:

Session::put('modules', $array);
Sign up to request clarification or add additional context in comments.

1 Comment

Yea okey true, men the foreach method does not work, i have tried sevral things to get the data out. EDIT Okey your code worked! i used $module->name not $module['name'], god damit! But thank you mate.

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.