Laravel 显示每个类别的最后一篇文章
我有两个模型,一个是Post,一个是Category //移民站Laravel 显示每个类别的最后一篇文章,laravel,Laravel,我有两个模型,一个是Post,一个是Category //移民站 public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('body'); $table->string('i
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->string('image');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
//迁移类别
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
如何在主页中仅显示每个类别的最后一篇文章?Hiren已接近尾声,但您需要从该类别转到其他类别,因为您的文章归该类别所有 或者,您可以反向工作:
$post = Post::latest()->whereHas('category', function($q) use($category_id) {
return $q->where('id', $category_id);
})->first();
要使其工作,您需要定义模型关系:
类别模型需要此功能:
public function posts()
{
return $this->hasMany(App\Post::class);
}
public function category()
{
return $this->belongsTo(App\Category::class);
}
Post模型需要此功能:
public function posts()
{
return $this->hasMany(App\Post::class);
}
public function category()
{
return $this->belongsTo(App\Category::class);
}
为了响应Alexey Mezenin,我们只需将回调传递给with,以定义我们希望为每个类别引入哪些帖子,并执行正确的即时加载
用最新帖子加载类别的一个有力解决方案是在类别模型中创建一个附加关系: 然后使用: 这将只生成2个对DB的查询
public function up()
{
Schema::create('news', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->unsignedInteger('author_id');
$table->unsignedInteger('category_id');
$table->string('subject');
$table->text('short');
$table->text('content');
$table->integer('view')->default(0);
$table->integer('status')->default(0);
$table->string('image');
$table->timestamps();
$table->foreign('author_id')
->references('id')->on('users')
->onDelete('cascade');
// $table->foreign('category_id')
// ->references('id')->on('categories')
// ->onDelete('cascade');
});
// Schema::enableForeignKeyConstraints();
}
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
在控制器中:
$latestpostlist = News::whereIn('created_at',function($query){
$query->select(DB::raw('max(created_at)'))
->from('news')
->groupBy('category_id');
})->get();
在你的情况下,新闻将被张贴。这个查询对我有用Try:$catList=Category::list'id'$post=post::其中'category\u id',$catList->groupby'category\u id'->latest->get@HirenGohel谢谢,但我对undefined方法Illumb\Database\Query\Builder::Listsy有此错误调用,您可以使用方法Pull。Laravel 5.3中删除了“方法列表”。Try:$catList=Category::pull'id'$post=post::其中'category\u id',$catList->groupby'category\u id'->latest->get;SQLSTATE[42000]:语法错误或访问冲突:1055“ex.posts.id”不在GROUP BY SQL中:从类别id在1、2中的帖子中选择*按类别id按顺序创建,按描述:$catList=category::pulk'id'->all@user5565240添加了有关模型关系定义的其他信息。只想说,此解决方案将只对一个类别进行post,如果您在循环中使用它,它将为100个类别创建101个查询。@OhGod为什么您添加了另一个解决方案,但它不起作用,因为它将只加载一篇文章,而不是每个类别加载一篇文章。SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“category.category\u id”SQL:select*从categories where categories.category\u id中按1,2的顺序创建desc@user5565240你不能用我的代码得到这个错误,你已经修改了它。请仔细检查。它在我的主页表格中给我空行,如果我添加了一个没有相关帖子的类别,这是非常有用的!!非常感谢你,阿列克西!它完全解决了我的范围问题!我试着保持冷静,运用我刚学到的东西哈哈。
public function up()
{
Schema::create('news', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->unsignedInteger('author_id');
$table->unsignedInteger('category_id');
$table->string('subject');
$table->text('short');
$table->text('content');
$table->integer('view')->default(0);
$table->integer('status')->default(0);
$table->string('image');
$table->timestamps();
$table->foreign('author_id')
->references('id')->on('users')
->onDelete('cascade');
// $table->foreign('category_id')
// ->references('id')->on('categories')
// ->onDelete('cascade');
});
// Schema::enableForeignKeyConstraints();
}
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
$latestpostlist = News::whereIn('created_at',function($query){
$query->select(DB::raw('max(created_at)'))
->from('news')
->groupBy('category_id');
})->get();