I am passing an array to a view from my controller.
Here is the controller function:
$notification = array(
'message' => 'Welcome Admin!',
'alert_type' => 'success',
);
return redirect('/home')->with('notification', $notification);
In my view:
<script>
@if(Session::has('notification'))//this line works as expected
var type = "{{ Session::get('alert_type', 'info') }}";
//but the type var gets assigned with default value(info)
switch(type){
case 'info':
toastr.info("{{ Session::get('message') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('message') }}");
break;
case 'success':
toastr.success("{{ Session::get('message') }}");
break;
case 'error':
toastr.error("{{ Session::get('message') }}");
break;
}
@endif
</script>
as you can see there's clearly something wrong with the way I am trying to access the array value in var type = "{{ Session::get('alert_type', 'info') }}";
EDIT- 1 : I tried doing
var type = "{{ Session::get('notification')->alert_type, 'info' }}";
switch(type){
case 'info':
toastr.info("{{ Session::get('notification')->message }}");
break;
case 'warning':
toastr.warning("{{ Session::get('notification')->message }}");
break;
case 'success':
toastr.success("{{ Session::get('notification')->message }}");
break;
case 'error':
toastr.error("{{ Session::get('notification')->alert_type }}");
break;
}
but now I get an error saying
Trying to get property of non-object (View: C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php) (View: C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php)
can anyone please help me with this?