如何使用 Laravel 的查询生成器按状态过滤帖子?
How can I filter posts by status with Laravel's query builder?
我正在开发一个包含用户和帖子的 Laravel 8 应用程序。
根据 posts
table 中的所有条目,我需要制作 2 个帖子列表,具体取决于它们是 活跃的 还是 待定。状态来自 post_statuses
table,列 id
和 status
.
在 Post 模型中我有:
class Post extends Model {
use HasFactory;
protected $fillable = [
'title',
'category_id',
'description',
'body'
'status'
];
}
在控制器中:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostsController extends Controller {
public $page_title = "Posts";
public function index(Request $request) {
$viewData = $this->loadViewData();
$viewData['page_title'] = $this->page_title;
$viewData['posts'] = $this->posts($request);
// send active posts to the view
return view('active_posts', $viewData);
}
public function posts(Request $request) {
$query = $request->input('query');
return Post::select (
"posts.title",
"posts.description",
"posts.body",
"categories.name as category_name",
"post_statuses.status as post_status",
)
->leftJoin("categories", "posts.category_id", "=", "categories.id")
->leftJoin("post_statuses", "posts.status", "=", "post_statuses.id")
// filter by status
->where('post_statuses.status', '=', 'active')
->orWhere('title', 'like', '%' . $query . '%')
->orWhere('description', 'like', '%' . $query . '%')
->orWhere('body', 'like', '%' . $query . '%')
->orWhere('categories.name', 'like', '%' . $query . '%')
->orWhere('post_statuses.status', 'like', '%' . $query . '%')
->orderBy('posts.id','DESC')
->paginate(10);
}
}
问题
在 active_posts
视图中,显示所有帖子,无论其状态如何。
我的错误在哪里?
我猜 orWhere
弄乱了你的查询。
这样试试:
public function posts(Request $request) {
$query = $request->input('query');
return Post::select (
"posts.title",
"posts.description",
"posts.body",
"categories.name as category_name",
"post_statuses.status as post_status",
)
->leftJoin("categories", "posts.category_id", "=", "categories.id")
->leftJoin("post_statuses", "posts.status", "=", "post_statuses.id")
// filter by status
->where('post_statuses.status', '=', 'active')
->where(function ($q) use($query){
$q->where('title', 'like', '%' . $query . '%')
->orWhere('description', 'like', '%' . $query . '%')
->orWhere('body', 'like', '%' . $query . '%')
->orWhere('categories.name', 'like', '%' . $query . '%')
->orWhere('post_statuses.status', 'like', '%' . $query . '%');
})
->orderBy('posts.id','DESC')
->paginate(10);
}
我正在开发一个包含用户和帖子的 Laravel 8 应用程序。
根据 posts
table 中的所有条目,我需要制作 2 个帖子列表,具体取决于它们是 活跃的 还是 待定。状态来自 post_statuses
table,列 id
和 status
.
在 Post 模型中我有:
class Post extends Model {
use HasFactory;
protected $fillable = [
'title',
'category_id',
'description',
'body'
'status'
];
}
在控制器中:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostsController extends Controller {
public $page_title = "Posts";
public function index(Request $request) {
$viewData = $this->loadViewData();
$viewData['page_title'] = $this->page_title;
$viewData['posts'] = $this->posts($request);
// send active posts to the view
return view('active_posts', $viewData);
}
public function posts(Request $request) {
$query = $request->input('query');
return Post::select (
"posts.title",
"posts.description",
"posts.body",
"categories.name as category_name",
"post_statuses.status as post_status",
)
->leftJoin("categories", "posts.category_id", "=", "categories.id")
->leftJoin("post_statuses", "posts.status", "=", "post_statuses.id")
// filter by status
->where('post_statuses.status', '=', 'active')
->orWhere('title', 'like', '%' . $query . '%')
->orWhere('description', 'like', '%' . $query . '%')
->orWhere('body', 'like', '%' . $query . '%')
->orWhere('categories.name', 'like', '%' . $query . '%')
->orWhere('post_statuses.status', 'like', '%' . $query . '%')
->orderBy('posts.id','DESC')
->paginate(10);
}
}
问题
在 active_posts
视图中,显示所有帖子,无论其状态如何。
我的错误在哪里?
我猜 orWhere
弄乱了你的查询。
这样试试:
public function posts(Request $request) {
$query = $request->input('query');
return Post::select (
"posts.title",
"posts.description",
"posts.body",
"categories.name as category_name",
"post_statuses.status as post_status",
)
->leftJoin("categories", "posts.category_id", "=", "categories.id")
->leftJoin("post_statuses", "posts.status", "=", "post_statuses.id")
// filter by status
->where('post_statuses.status', '=', 'active')
->where(function ($q) use($query){
$q->where('title', 'like', '%' . $query . '%')
->orWhere('description', 'like', '%' . $query . '%')
->orWhere('body', 'like', '%' . $query . '%')
->orWhere('categories.name', 'like', '%' . $query . '%')
->orWhere('post_statuses.status', 'like', '%' . $query . '%');
})
->orderBy('posts.id','DESC')
->paginate(10);
}