在 HomeController 中按类别名称获取帖子 (Laravel 8)
Get posts by category's name in HomeController (Laravel 8)
详细解释一下:
例如,我正在创建带有类别的 posts,但是我将其保存在不同的 table (post_categories) 中,这意味着我的 posts 和类别通过他们的 ID(属于 ToMany)。现在在我的 HomeController 中,我想按类别名称显示 posts,
例如:$posts = post::where([['status',1],['category', 'electronics'])->orderBy('created_at', 'DESC')->limit(1)->get();
我想按类别名称而不是类别 ID 显示我的 post
我试过了,但它并没有真正起作用,我想我忘记了什么或者我使用了错误的方法,请帮助我
代码
家庭控制器
public function index()
{
$posts = post::where([['status',1],//here is category missing])->orderBy('created_at', 'DESC')->limit(1)->get();
$categories = Category::all();
return view('user.home', compact('posts', 'categories'));
}
public function category(category $category)
{
$posts = $category->posts();
return view('user.home',compact('posts'));
}
类别模型
use HasFactory;
public function posts()
{
return $this->belongsToMany('App\Models\user\post','category_posts')->orderBy('created_at','DESC')->paginate(5);
}
public function getRouteKeyName()
{
return 'slug';
}
post 型号
use HasFactory;
public function categories()
{
return $this->belongsToMany('App\Models\user\category','category_posts')->withTimestamps();;
}
public function getRouteKeyName()
{
return 'slug';
}
数据库
Posts_table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title',1000);
$table->string('subtitle',1000);
$table->string('slug',1000)->nullable();
$table->text('body');
$table->boolean('status')->nullable();
$table->integer('posted_by')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
}
Categories_table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
category_posts
public function up()
{
Schema::create('category_posts', function (Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedInteger('category_id');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
}
public function index()
{
$posts = post::with('categories')->where([['status',1],//here is category missing])->orderBy('created_at', 'DESC')->limit(1)->get();
$categories = Category::all();// don't need this line if you don't need all categires
return view('user.home', compact('posts', 'categories'));
}
我已经有了答案
我删除了关系并将位置添加到一个值以便 c
详细解释一下:
例如,我正在创建带有类别的 posts,但是我将其保存在不同的 table (post_categories) 中,这意味着我的 posts 和类别通过他们的 ID(属于 ToMany)。现在在我的 HomeController 中,我想按类别名称显示 posts,
例如:$posts = post::where([['status',1],['category', 'electronics'])->orderBy('created_at', 'DESC')->limit(1)->get();
我想按类别名称而不是类别 ID 显示我的 post
我试过了,但它并没有真正起作用,我想我忘记了什么或者我使用了错误的方法,请帮助我
代码
家庭控制器
public function index()
{
$posts = post::where([['status',1],//here is category missing])->orderBy('created_at', 'DESC')->limit(1)->get();
$categories = Category::all();
return view('user.home', compact('posts', 'categories'));
}
public function category(category $category)
{
$posts = $category->posts();
return view('user.home',compact('posts'));
}
类别模型
use HasFactory;
public function posts()
{
return $this->belongsToMany('App\Models\user\post','category_posts')->orderBy('created_at','DESC')->paginate(5);
}
public function getRouteKeyName()
{
return 'slug';
}
post 型号
use HasFactory;
public function categories()
{
return $this->belongsToMany('App\Models\user\category','category_posts')->withTimestamps();;
}
public function getRouteKeyName()
{
return 'slug';
}
数据库
Posts_table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title',1000);
$table->string('subtitle',1000);
$table->string('slug',1000)->nullable();
$table->text('body');
$table->boolean('status')->nullable();
$table->integer('posted_by')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
}
Categories_table
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
category_posts
public function up()
{
Schema::create('category_posts', function (Blueprint $table) {
$table->unsignedBigInteger('post_id');
$table->unsignedInteger('category_id');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
}
public function index()
{
$posts = post::with('categories')->where([['status',1],//here is category missing])->orderBy('created_at', 'DESC')->limit(1)->get();
$categories = Category::all();// don't need this line if you don't need all categires
return view('user.home', compact('posts', 'categories'));
}
我已经有了答案 我删除了关系并将位置添加到一个值以便 c