Reputation: 117
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
Upvotes: 0
Views: 200
Reputation: 1938
'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 {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks.
Upvotes: 1