简体   繁体   English

如何在javascript中放置laravel echo函数

[英]how to put laravel echo function in javascript

How to use echo in strings in javascript datatable如何在javascript数据表中的字符串中使用echo

className:'text-center', 
data:'stage', 
name:'stage',
render: function(data)
{
    return '<td class="text-center">{{trans('stage.'' + data.stage + ')}}</td>';
}

error: syntax error, unexpected ''+ data.stage +'' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')'错误:语法错误,意外的 ''+ data.stage +'' (T_CONSTANT_ENCAPSED_STRING),期待 ',' 或 ')'

I have tried escaping using backlash it still doesnt look right.我试过使用反弹来逃避它仍然看起来不对。

<td class="text-center">{{trans('stage.'.$sub->stage)}}</td> 
//this is what i want to display if i were to use laravel blade

Blade is rendered before you hit the page. Blade 在您点击页面之前被渲染。 So you cannot do what you're trying to, like this.所以你不能做你想做的事,就像这样。 I would recommend using xDebug to see how this works more consistently我建议使用 xDebug 来查看它如何更一致地工作

If you want to include a translated variant of text in the variable stage , you either need to fetch some HTML and pass it back rendered, via an ajax request for example如果要在变量stage包含文本的翻译变体,则需要获取一些 HTML 并将其传递回渲染,例如通过ajax请求

OR或者

You can write/use a javaScript helper which will translate text for you.您可以编写/使用一个 javaScript 助手来为您翻译文本。

OR (best)或(最佳)

You can write an accessor/getter on stage which will translate it before it's received by your DataTable您可以在stage上编写一个访问器/获取器,它会在您的 DataTable 接收到它之前对其进行翻译

This would mean writing the following on the Model where the attribute stage comes from.这意味着在属性stage来自的Model上写下以下内容。 Please note, prefixed Method name with 'get' and 'Attribute' as the suffix.请注意,以“get”和“Attribute”为后缀的前缀方法名称。

    public function getStageAttribute()
    {
        return trans($this->stage);
    }

For more information please see https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor有关更多信息,请参阅https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM