0
にアヤックスから変数を渡し、私は、Ajaxとコメントのリストを持っているが、それは仕事doesntの:Laravel 5.1 - 私のコントローラ
CommentList.js
$(document).ready(function(){
var tablaDatos = $("#comentos");
var dato2= $("#article_id").val();
var route = "http://localhost:8000/commentList/"+dato2;
$.get(route, function(res){
$(res).each(function(key,value){
tablaDatos.append(value);
});
});
});
article.blade.phpを
@foreach($comments as $comment)
<li id="comentos">
<input type="hidden" name="article_id" id="article_id" value="{{$article->id}}">
</li>
@endforeach
FrontController.php
public function listing($id)
{
// store the comments list
$list = [];
// retrieve comments
$article = Article::where('article_id', $id)->first();
$comments = Comment::where('article_id', $article->id)->get();
// render each comment and store it
foreach ($comments as $comment) {
$html = view('layouts.comment-template')
->with('comment', $comment)
->render();
$list[] = $html;
}
// return a JSON array of the comments list
return response()->json($list);
}
私は、 "article.blade.php" 私のビューに "$の記事を" 合格ので、 "$ article-> IDを" 仕事も
私はその問題があると思う$ article-> ID入力doesntのパスcommentList.js(間違ったルート?)と私のコントローラ、多分問題に
ルート
Route::get('commentList/{id}','[email protected]');
私の変数IDを渡すための最良の方法はありますか?
あなたのコンソールにはどんな反応がありますか? 「うまくいかない」というのは十分ではありません。もしあなたがAjaxリクエストをしているのであれば、サーバはあなたが持っている問題の種類を示すものを送り返すべきです。 –
私は私のコンソールをチェックし、エラーはArticle :: where( 'article_id'、$ id)です。正しいのはArticle :: where( 'id'、$ id)です。 –