I need to display posts by tags. My solution works for a single tag like this:
route:
Route::get('/posts', [PostController::class, 'index'])->middleware('auth');
Post model filter:
public function scopeFilter($query, array $filters) { if ($filters['tag'] ?? false) { $tagId = Tag::where('name', $filters['tag'])->first()->id; $query->whereHas('tags', function($q) use ($tagId) { $q->where('tag_id', $tagId); }); } }
Method index of PostController:
public function index() { return view('posts', [ 'posts' => Post::latest()->filter(request(['tag']))->get() ]); }
This code applies to the following URL: "http://127.0.0.1:8000/posts/?tag=test". But I need to find a way to search for posts with more tags, for example I want to find posts with tags "test" and "unit". To do this I want to use a URL like this: "http://127.0.0.1:8000/posts/?tag=test&unit". I'm stuck because I thought "request(['tag'])" would return "test&unit" but it only returns "test". Is it possible to somehow get the "unit" tag from this request?
GET
requests using query strings can accept multiple parameters. instead of?tag=test&unit
(which won't really work anyway, since&unit
will be parsed as$request->input('unit')
, and will benull
,&
are reserved characters), you can send it as:On the backend, when you access
request()->input('tags')
, you get the following array:So, put them together:
whereIn()
request()->input('tags', [])
to access?tags[]=...&tags[]=...
, if not Provided is an empty array.