[英]Use of undefined constant post - assumed 'post' - php code with html
您知道為什么這樣的代碼"<a href="{{route('posts.show', ['id' => 'post.id', 'slug' => post.slug])}}" class="btn btn-primary text-white">More</a>"
下面代碼中的"<a href="{{route('posts.show', ['id' => 'post.id', 'slug' => post.slug])}}" class="btn btn-primary text-white">More</a>"
顯示了使用未定義的常量帖子-假設為“帖子”?
$.each(result, function(index, post) {
newPosts += '<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">\n' +
' <div class="card box-shaddow">\n' +
+' <div class="card-body">\n' +
+' <h5 class="card-title h6 font-weight-bold text-heading-blue">'+post.name+'</h5>\n' +
+' </div>\n' +
' <div class="card-footer d-flex justify-content-between align-items-center">\n' +
' <a href="{{route('posts.show', ['id' => 'post.id', 'slug' => post.slug])}}" class="btn btn-primary text-white">More</a>
' <span class="font-weight-bold font-size-sm text-heading-blue"> </span>\n' +
' </div>\n' +
' </div>\n' +
' </div>';
});
您不能在調用Blade的route()
函數時將Javascript post
對象用作變量。 Blade會在將模板發送到瀏覽器之前在服務器上呈現該模板,而您的Javascript代碼將在以后的訪問者在其計算機上執行。
一種選擇是讓Blade使用虛擬值填充您可以在Javascript中使用的變量:
var placeholder = "{{route('posts.show', ['id' => 123, 'slug' => 'demo-slug'])}}";
$.each(result, function(index, post) {
var url = placeholder.replace(123, post.id).replace('demo-slug', post.slug);
newPosts += '[...]'
+ '<a href="' + url + '" class="btn btn-primary text-white">More</a>'
+ '[...]';
});
用作占位符的值取決於路由的外觀以及所使用的驗證(如果有)。 只要確保您使用的占位符文本都不是URI的一部分即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.