简体   繁体   English

Laravel-如何“捕获”传递给刀片的 Javascript 变量。php 视图

[英]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.

相关问题 Laravel-如果我将变量传递给控制器​​并保存在数据库中,如何在 javascript 中使用 switch case - Laravel- How to use switch case in javascript if I passed the variable to controller and saved in database 获取jQuery函数内部blade.php Laravel的返回参数 - Get return parameter of jQuery function inner blade.php Laravel React-Laravel(7.2)组件未显示在刀片中。php - React-Laravel(7.2) Component not showing in blade.php Laravel-如何从刀片文件将php变量传递给javascript函数 - Laravel - How to pass a php variable to a javascript function from a blade file 如何在我的blade.php中为角度$ scope分配值 - How to assign value to angular $scope in my blade.php 如何在blade.php 中使用来自外部.js 文件的代码? - How to use code from external .js files in blade.php? 如何在laravel刀片中动态打印JavaScript变量 - How to print JavaScript variable dynamically in laravel blade 如何将Javascript变量设置为laravel刀片文件? - How to set javascript variable to laravel blade file? 在Laravel Blade Template中使用JavaScript将PHP变量与HTML存储在一起 - Store PHP variable with HTML in JavaScript in Laravel Blade Template 在一个刀片中组合 2 个不同的脚本后,脚本不起作用。php 文件(laravel) - Script doesn't work after combining 2 different scripts in one blade.php file (laravel)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM