[英]Laravel- How to "catch" a variable with Javascript passed to a blade.php view
so i'm currently working on a project and i want to add a calendar to one view, i'm using FullCalendar, the calendar is shown in the view but i'm currently trying to add events to that calendar, those events are going to be "pulled" from my database into the calendar to be displayed, now the problem is that when i pass the array with the events to the blade.php view that contains the calendar a error comes up saying that the variable "events" is undefined and i don't know if i'm doing something wrong, here's the code:所以我目前正在处理一个项目,我想将日历添加到一个视图,我正在使用 FullCalendar,日历显示在视图中,但我目前正在尝试向该日历添加事件,这些事件正在进行中要从我的数据库“拉”到要显示的日历中,现在的问题是当我将带有事件的数组传递给 blade.php 包含日历的视图时出现错误,说变量“事件”是未定义,我不知道我是否做错了什么,这是代码:
RegisteredUserController.php RegisteredUserController.php
$events = array();
$citas = Cita::all();
foreach($citas as $cita){
$events[] = [
'title' => $cita->paciente_id,
'start' => $cita->hora_de_inicio,
'end' => $cita->hora_de_finalizacion
];
}
return redirect(RouteServiceProvider::HOME)->with(['events' => $events]);
RouteServiceProvider.php RouteServiceProvider.php
public const HOME = '/home';
homeView.blade.php ( This is where i get the error, in the line where i assign it to citas ) homeView.blade.php(这是我收到错误的地方,在我将其分配给 citas 的行中)
<div id="calendar">
</div>
<script>
$(document).ready(function(){
var citas = @json($events);
$('#calendar').fullCalendar({
header: {
left: 'month, agendaWeek, agendaDay',
},
events: citas
})
});
</script>
routes / web.php路线 / web.php
Route::get('/home', function (){
return view('homeView');
})->name('homeView');
Not sure if it's helpful but i'm using Laravel's Breeze to handle the authentication of users, so when an user is aunthenticated they're redirected to the page where the calendar is.不确定它是否有帮助,但我正在使用 Laravel 的 Breeze 来处理用户的身份验证,因此当用户经过身份验证时,他们将被重定向到日历所在的页面。
i've tried the following我试过以下
RegisteredUserController.php RegisteredUserController.php
return redirect(RouteServiceProvider::HOME, compact('events));
This one makes the whole calendar disappear:这使整个日历消失:
homeView.blade.php homeView.blade.php
var citas = {{ Session::get('events') }};
Thank you in advance.先感谢您。
the return should be like this回报应该是这样的
return view('path_of_the_blade', compact('events'));
So then in View, you can use那么在视图中,您可以使用
<script>
var citas = @json($events);
</script>
On you controller, why don't you just simply use:在你 controller 上,你为什么不简单地使用:
return view('homeView')->with(['events' => $events]);
or simplier:或更简单:
return view('homeView', compact('events'));
or even甚至
return view('homeView', ['events']);
This would surely pass the variable events
on the blade file.这肯定会传递刀片文件上的变量
events
。
Then on your blade file, you will just reference it as:然后在您的刀片文件中,您只需将其引用为:
var citas = {{ $events }};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.