0

Trying to set and get session in laravel. In my controller I'm converting an array to string which doesn't throw an error if printed, however froim the error looks like session is getting an array instead of a string. Controller:

 public function  getQuestions(){

    $questions = DB::table('questions')->orderBy(DB::raw('RAND()'))->get();

    $questions= json_decode(json_encode($questions), true);

    Session::put('questions', $questions);

    return view('test', array('questions' => $questions));
}

Blade:

{{ Session::get('questions') }}

An error:

htmlentities() expects parameter 1 to be string, array given
0

1 Answer 1

1

'questions' is collection of objects. You can't print objected directly using {{placeholder}}. Whenever you use {{ placeholder}} laravel blade engine assume that it is a string. So it passes that varaible through htmlentities functions. So the error comes. If you really need to print this, you can write a foreach loop. Here is sample

@foreach(Session::get('questions') as $question)
         {{$question-> attribute _to_print}}
      @endforeach

Blade tutorial

Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks.

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

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.