I'm using OctoberCMS based on Laravel.
I'm trying to capture segments of the URL to pass into a Scope Function to filter Database Results.
Visiting localhost/user/john/nature would parse and filter back those results.
URL → Identifiers → Param Variables → Scope → Database Results
Page
[builderDetails builderUser]
identifierValue "{{ :username }}"
[builderDetails builderCategory]
identifierValue "{{ :category }}"
[builderList]
scope = "scopeApplyType"
url = /user/:username?/:category?/:page?
Model Scope
I want to filter Database Results using URL Identifers :username and :category.
public function scopeApplyType($query) {
$params = ['username' => $username, 'category' => $category];
return $query->where($params);
}
Get Identifiers
This will output the requested identifers in the URL, in routes.php
Route::get('user/{username?}/{category?}', function ($username = null, $category = null) {
echo $username;
echo $category;
});
Output
localhost/user/john/nature
john
nature
Soultion?
A Route::get() won't work, I need something inside or passed to the Scope to define the params variables.
Something like:
$username = '{username?}';
$username = request()->url('username');
$username = $this->param('username'); //Components)
$username = $this->route('username');
$username = \Route::current()->getParameter('username');
All return null or error.
Like the way you would normally parse a Query
$param = "username=john&category=nature";
$username = $category = '';
parse_str($param);
echo $username;
echo $category;
Or similar to Request Segment
$username = request()->segment(2); //user/:username
segment(2) is a static location unlike {:category} which can change position on different URL's.
$request->route('parameter_name')or$request->route()->parameters()?request()should give you it.request()->route()