Php LARAVEL-无法在控制器方法内调用函数
下面是CommentController的注释方法Php LARAVEL-无法在控制器方法内调用函数,php,mysql,laravel,laravel-5.2,laravel-5.3,Php,Mysql,Laravel,Laravel 5.2,Laravel 5.3,下面是CommentController的注释方法 public function comment(Request $request) { $comments = DB::table('comments') ->join('answers', 'answers.id' , '=', 'comments.answer_id') ->join('users' , 'users.id' , '=',
public function comment(Request $request)
{
$comments = DB::table('comments')
->join('answers', 'answers.id' , '=', 'comments.answer_id')
->join('users' , 'users.id' , '=', 'comments.user_id')
->where('answers.id', '=' , '9')
->where('parent_id', '0')
->select('comments.comment as comment',
'comments.id as comment_id',
'comments.created_at as created_at',
'comments.parent_id as parent_id',
// 'answers.aanswer as answer',
'answers.id as ans_id')
->orderBy('created_at', 'desc')
->get();
foreach ($comments as $comment) {
echo $comment->comment_id.$comment->comment.'<br>';
return $this->testingComment();
}
public function testingComment(){
echo "testing comments function";
}
}
公共功能注释(请求$Request)
{
$comments=DB::表('comments')
->join('answers','answers.id','=','comments.answers\u id'))
->join('users','users.id','=','comments.user_id'))
->其中('answers.id'、'='、'9')
->其中('parent_id',0')
->选择('comments.comment as comment',
“comments.id作为comment_id”,
'comments.created_at as created_at',
“comments.parent\u id作为parent\u id”,
//'answers.aanswer as answer',
‘answers.id作为ans_id’)
->orderBy('created_at','desc')
->get();
foreach($comments作为$comment){
echo$comment->comment_id.$comment->comment.“
”;
返回$this->testingComment();
}
公共功能测试注释(){
echo“测试注释功能”;
}
}
我有这个foreach循环,我只是不想在循环中调用这个testingComment函数,但它不起作用。
我正在构建一个嵌套注释系统,我想调用foreach循环中的函数,以便在父id匹配时呈现子和子子级注释。请复制并粘贴以下代码。它将正常工作,并且您遗漏了一些
:
public function comment(Request $request)
{
$comments = DB::table('comments')
->join('answers', 'answers.id' , '=', 'comments.answer_id')
->join('users' , 'users.id' , '=', 'comments.user_id')
->where('answers.id', '=' , '9')
->where('parent_id', '0')
->select('comments.comment as comment',
'comments.id as comment_id',
'comments.created_at as created_at',
'comments.parent_id as parent_id',
// 'answers.aanswer as answer',
'answers.id as ans_id')
->orderBy('created_at', 'desc')
->get();
foreach ($comments as $comment) {
echo $comment->comment_id.$comment->comment.'<br>';
return $this->testingComment();
}
}
public function testingComment(){
echo "testing comments function";
}
公共功能注释(请求$Request)
{
$comments=DB::表('comments')
->join('answers','answers.id','=','comments.answers\u id'))
->join('users','users.id','=','comments.user_id'))
->其中('answers.id'、'='、'9')
->其中('parent_id',0')
->选择('comments.comment as comment',
“comments.id作为comment_id”,
'comments.created_at as created_at',
“comments.parent\u id作为parent\u id”,
//'answers.aanswer as answer',
‘answers.id作为ans_id’)
->orderBy('created_at','desc')
->get();
foreach($comments作为$comment){
echo$comment->comment_id.$comment->comment.“
”;
返回$this->testingComment();
}
}
公共功能测试注释(){
echo“测试注释功能”;
}
因为您是在函数中定义函数,请尝试下面的代码
公共功能注释(请求$Request){
$comments=DB::table('comments'))
->join('answers','answers.id','=','comments.answers\u id'))
->join('users','users.id','=','comments.user_id'))
->其中('answers.id'、'='、'9')
->其中('parent_id',0')
->选择('comments.comment as comment'、'comments.id as comment\u id'、'comments.created\u at as created\u at'、'comments.parent\u id as parent\u id',
//'answers.aanswer as answer',
‘answers.id作为ans_id’)
->orderBy('created_at','desc')
->get();
foreach($comments作为$comment){
echo$comment->comment_id.$comment->comment.“
”;
返回$this->testingComment();
}
}
公共功能测试注释(){
echo“测试注释功能”;
}
您的另一个函数在comment函数中,这很可能是它没有做任何事情或中断的原因
public function comment(Request $request)
{
$comments = DB::table('comments')
->join('answers', 'answers.id' , '=', 'comments.answer_id')
->join('users' , 'users.id' , '=', 'comments.user_id')
->where('answers.id', '=' , '9')
->where('parent_id', '0')
->select('comments.comment as comment',
'comments.id as comment_id',
'comments.created_at as created_at',
'comments.parent_id as parent_id',
// 'answers.aanswer as answer',
'answers.id as ans_id')
->orderBy('created_at', 'desc')
->get();
foreach ($comments as $comment) {
echo $comment->comment_id.$comment->comment.'<br>';
return $this->testingComment();
}
}
public function testingComment()
{
echo "testing comments function";
}
公共功能注释(请求$Request)
{
$comments=DB::表('comments')
->join('answers','answers.id','=','comments.answers\u id'))
->join('users','users.id','=','comments.user_id'))
->其中('answers.id'、'='、'9')
->其中('parent_id',0')
->选择('comments.comment as comment',
“comments.id作为comment_id”,
'comments.created_at as created_at',
“comments.parent\u id作为parent\u id”,
//'answers.aanswer as answer',
‘answers.id作为ans_id’)
->orderBy('created_at','desc')
->get();
foreach($comments作为$comment){
echo$comment->comment_id.$comment->comment.“
”;
返回$this->testingComment();
}
}
公共功能测试注释()
{
echo“测试注释功能”;
}
这不是在comment
函数中声明的testingComment
吗?该代码几乎不可读。错误是什么?返回的错误是什么?根据代码的模式,显然您已经在comment()
函数中声明了函数。将它移出它。我使用ajax获取它,所以我得到了这个-POST 500(内部服务器错误)。。。我不能在foreach循环中调用任何简单的函数吗?
public function comment(Request $request)
{
$comments = DB::table('comments')
->join('answers', 'answers.id' , '=', 'comments.answer_id')
->join('users' , 'users.id' , '=', 'comments.user_id')
->where('answers.id', '=' , '9')
->where('parent_id', '0')
->select('comments.comment as comment',
'comments.id as comment_id',
'comments.created_at as created_at',
'comments.parent_id as parent_id',
// 'answers.aanswer as answer',
'answers.id as ans_id')
->orderBy('created_at', 'desc')
->get();
foreach ($comments as $comment) {
echo $comment->comment_id.$comment->comment.'<br>';
return $this->testingComment();
}
}
public function testingComment()
{
echo "testing comments function";
}