在HTML响应中使用碳

发布于 2025-01-19 21:42:33 字数 1377 浏览 0 评论 0原文

在下面的代码中,我想显示数据更新后几个小时前的更新,但是 ' + $item.updated_at->diffInHours(now()) + ' 给出以下错误 未捕获的语法错误:预期表达式,得到 '>' 它指向 -> 任何建议表示

赞赏,谢谢

index.blade.php

<script>
...

function fetchAllNames() {
        var _url = '{{ route("names.fetch.route") }}';
        $.ajax({
            type: "GET",
            url: _url,
            dataType: "json",
            success: function(response) {                        
                $('tbody').html("");
                $.each(response.name, function($key, $item) {
                    $('tbody').append('<tr>\
                        <td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
                        <td>' + $key + '</td>\
                        <td>' + $item.name + '</td>\                            
                        <td><label id="timeLabel">' + $item.updated_at->diffInHours(now()) + '</label></td>\
                    \</tr>');
                });
            }
        });               
    }

...
</script>

CustomController.php

public function FetchName() 
{ 
    $name = CandidateName::all(); 
    return response()->json(['name'=>$name]); 
}

In the below code, I want to show updated some hours ago since the data is updated, but ' + $item.updated_at->diffInHours(now()) + ' giving following error Uncaught SyntaxError: expected expression, got '>' It is pointing at -> operator

Any Suggestion is appreciated, Thanks

index.blade.php

<script>
...

function fetchAllNames() {
        var _url = '{{ route("names.fetch.route") }}';
        $.ajax({
            type: "GET",
            url: _url,
            dataType: "json",
            success: function(response) {                        
                $('tbody').html("");
                $.each(response.name, function($key, $item) {
                    $('tbody').append('<tr>\
                        <td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
                        <td>' + $key + '</td>\
                        <td>' + $item.name + '</td>\                            
                        <td><label id="timeLabel">' + $item.updated_at->diffInHours(now()) + '</label></td>\
                    \</tr>');
                });
            }
        });               
    }

...
</script>

CustomController.php

public function FetchName() 
{ 
    $name = CandidateName::all(); 
    return response()->json(['name'=>$name]); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2025-01-26 21:42:33

假设 $item.updated_at 是一个带有日期时间的字符串,例如“2022-04-01 12:12:00”。
您可以定义一个 javascript 函数

function diffInHoursToNow(dateStr){
   var inputDate = new Date(Date.parse(dateStr));
   var diff = new Date().getTime() - inputDate.getTime();
   //depending on what you want you can round this off with 
   //Math.floor(diff/3600)/1000 or similar 
   return diff/3600000;
}

,然后在生成表行时可以调用

<td><label id="timeLabel">' + diffInHoursToNow($item.updated_at) + '</label></td></tr>'

虽然它是有效的并且有时很有用,但您可能不想使用 $ 来启动您的 javascript 变量名称($key 和 $item)。这将有助于消除调用 php 函数的诱惑。

Assuming $item.updated_at is a string with a datetime such as '2022-04-01 12:12:00'.
You can define a javascript function

function diffInHoursToNow(dateStr){
   var inputDate = new Date(Date.parse(dateStr));
   var diff = new Date().getTime() - inputDate.getTime();
   //depending on what you want you can round this off with 
   //Math.floor(diff/3600)/1000 or similar 
   return diff/3600000;
}

and then when generating your table row you can call

<td><label id="timeLabel">' + diffInHoursToNow($item.updated_at) + '</label></td></tr>'

Although it's valid and can be useful on occasion, you probably don't want to use $ to start your javascript variable names ($key and $item). This will help to remove the temptation to call php functions.

最偏执的依靠 2025-01-26 21:42:33

您不能在对Ajax的响应中使用PHP,
您有两个选择是否在PHP中处理
在返回响应或使用之前可以通过JS处理它,
另外,请检查此 moment.js库其JavaScript库操纵日期的Javascript库,例如carbon library

应该应该像那样
在渲染中你html

var now = moment(new Date()); //todays date
var end = moment($item.updated_at); // another date

 $('tbody').append('<tr>\
                        <td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
                        <td>' + $key + '</td>\
                        <td>' + $item.name + '</td>\                            
                        <td><label id="timeLabel">' + moment.duration(now.diff(end))  + '</label></td>\
                    \</tr>');

you cant use PHP in the response to ajax,
you have two options whether to handle it in PHP
before return response or use can handle it via js,
also please check this moment.js Library its Javascript library that manipulates dates like Carbon library

should be something like that
at render you html

var now = moment(new Date()); //todays date
var end = moment($item.updated_at); // another date

 $('tbody').append('<tr>\
                        <td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
                        <td>' + $key + '</td>\
                        <td>' + $item.name + '</td>\                            
                        <td><label id="timeLabel">' + moment.duration(now.diff(end))  + '</label></td>\
                    \</tr>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文