相当标准的设置:
表"Posts",表“类别”“枢轴”posts_categories
/************** CATEGORY MODEL *********/
class Category extends Eloquent
{
/** standard code**/
public function posts()
{
return $this->belongsToMany('Post','posts_categories');
}
}
/************* POSTS MODEL ***********
你好,我用laravel创建了一个网站。我的职位与类别有关的id。像这样
table post
id
title
category_id
table category
id
name
desc
我想要显示一个档案与类别,所以我需要从数据库获得所有的帖子。但我把t know how to get category bycategory_id`放在邮政桌上
我知道数据库中的所有数据都是数组,我不知道如何创建一个关系,以便从post表中的id获取类别名称。
我有一个post Post和Category类别的模型,以及控制器PostController和CategoryCon
我有一个laravel应用程序,我试图在主索引页面上显示所有用户的所有帖子。然而,我在尝试查询所有用户的所有帖子时遇到了问题。下面的代码只获取第一个用户的所有帖子。 下面是查询 //grab all users from the database and store it in the users variable
$users = User::all();
//grab all posts from all users. list by most recent in descending order store in recentPosts variable
$recentPosts =
我使用自引用模型来构建嵌套类别:
class Category extends Model {
public function parent()
{
return $this->belongsTo('Category', 'parent_id');
}
public function children()
{
return $this->hasMany('Category', 'parent_id');
}
// recursi
我有两个多对多的关系表:帖子和类别。一个帖子可以有多个类别。我的问题是,我如何显示帖子的分类列表?
就像这样:
我的帖子1 (cat1,cat2,cat3)
我的帖子2 (cat2,cat3)
我的帖子3 (cat1)
我已经尝试过这些方法;
// Create post object
$p = new Post();
// Get 30 posts
$p->get(30);
// Loop through all posts
foreach ($p as $post)
{
// Get the current user's group
$post->c
我正在给一个Next.js博客添加内容,并在首页上查询一些帖子。尽管我可以使用一个简单的布尔值来.filter()有特色的帖子,但我似乎不能理解如何在我的帖子结构中获取匹配类别的帖子。 这是我对特色帖子的查询示例。 {posts.filter((posts) => (posts.fields.postDestaque)) // it filters posts that have 'postDestaque' false or null
.slice(0, 6) // cuts off the excess
.map((post, i) => { /