Table 使用 yajra/laravel-datatables 为一篇文章带来所有评论

马赫迪·阿卜杜勒萨希布

当试图让所有评论一个文章Article::first(),但first()只带了第一篇文章中我尝试使用find()类似

$comments = Article::find()-> commentsArticle()->with('articles');
return Datatables::of($comments)

我收到错误,所以我如何传递一个值来查看一篇文章的所有评论,或者我可以不使用 find()

文章模型

class Article extends Model{

 public $table = 'articles';

 public function commentsArticle() {
     return $this->hasMany('App\Comment');
     }

 }

控制器

enter code here

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;

use App\Article;
use App\Comment;


class CommentController extends Controller{


   public function commentsForOne Article()
  {
    $comments = Article::all()->commentsArticle->with('articles');

            return Datatables::of($comments)->make(true);
  }

}

我得到的最后一个错误

ErrorException (E_DEPRECATED)
Non-static method Yajra\Datatables\Datatables::collection() should       
not be called statically

我希望能找到任何类似的想法或例子来帮助我学习

EddyTheDove

您正在尝试获取带有评论的第一篇文章。

public function commentsForOneArticle($id)
{
    $article = Article::fine($id);

    //check if article exists to avoid errors
    if ( $article ) {
        return Datatables::of(Comment::where('article_id', $article->id))->make(true);
    }

    return "no article with id" . $id;
}

这只是一个例证。但似乎您首先需要了解 Eloquent 的工作原理。观看免费的Laracast https://laracasts.com/series/laravel-from-scratch-2017/episodes/7

对于路由,您可以像这样定义路由:

Route::get('comments/{article_id}', 'ArticleController@commentsForOneArticle');

并在 Ajax 中调用它

$.ajax({url: "/comments/1", 
    success: function(result){
        //do stuff here
    },
    error: function(error) {
        console.log(error)
    }
});

所有这些只是一个指南,而不是解决方案。

编辑

与用户一次性获取数据

$article = Article::with('user')->find($id);
//will include all the fields from user and article

Comments & author要获得评论作者的名字,需要在评论模型中定义关系

public function user() {
    return $this->belongsTo(User::class);
}

然后变成这样

if ( $article ) {
    return Datatables::of(Comment::with('users')->where('article_id', $article->id))->make(true);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在一篇文章中使用2个中间件功能

给定一篇文章的 DOI,如何使用 python 获取域名?

我正在尝试使用 Flask 删除一篇文章,但出现 KeyError

使用wp_query检查第一篇文章

如何只编辑一篇文章而不是迭代循环所有文章?

我在使用datatables.net时我的DataTables有问题

单击自定义分类法时,如何使用第一篇文章数据在同一页面上重定向?

共同作者加上Wordpress插件-列出同一篇文章的所有作者

Meteor - 发布所有作者且仅发布最后一篇文章的最佳方法

让columnFilter插件与DataTables一起使用

无法使DataTables与Jade一起使用

为什么get_posts函数显示除一篇文章以外的所有文章?

仅在 Vanilla JavaScript 中附加在第一篇文章上的评论

如何从一篇文章中获取所有段落而不是一个段落?

从DataTables-Table获取ID

DataTables 函数- table.row.add ,不使用表格内的按钮(javascript)

Mongodb:为每个作者ID查找一篇文章

如何在 MBootstrap 中为 DataTables 使用异步 .update() 方法?

使用jQuery检查DataTables数据是否为空或为null

使用具有非表格布局的jQuery Datatables

使用datatables的带有可选行的html表

在带有AJAX数据源的DataTables中使用列名

使用jQuery DataTables时禁用最后一列的排序

jQuery .load()函数不能与DataTables一起使用?

将Laravel存储库与Datatables一起使用

将datatables.net与javascript一起使用

是否有可能获得一篇文章被引用的次数?

如何滚动到具有相同类的下一篇文章 ON SCROLL

WordPress的第一篇文章只在首页上有所不同