This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
How can I grab the Identifier from the URL and pass it to the Model Scope?
I have been stuck on this problem for several days.
URL: localhost/user/Matt/general
Identifiers: /user/:username?/:category?/:page?
In my custom Component or Page, I can get the Identifiers into a varaible using:
$username = $this->param('username'); //Matt (:username?)
$category = $this->param('category'); //general (:category?)
Page
[builderList]
...
scope = "scopeApplyUser"
...
{% component 'builderList' %}
Model
Now how to get it into the Scope to filter results?
// return results that match username and category
public function scopeApplyUser($query, $username = null, $category = null)
{
$params = ['username' => $username, 'category' => $category];
return $query->where($params);
}
I am not an expert, but I think there is no way, but hey.. luckily this builderList component does not really much, so why don't just use your own function:
function onStart()
{
try {
// adapt your params and query
$this['xxx'] = YourModel::where('slug',$this->param('yourslug'))->firstOrFail();
}
catch (ModelNotFoundException $ex) {
return Redirect::to('/404');
}
}
And then in your page:
{% for x in xxx %}
{{ x.anyAttribute }}
{% endfor %}
stefan19666 said:
I am not an expert, but I think there is no way, but hey.. luckily this builderList component does not really much, so why don't just use your own function:
function onStart() { try { // adapt your params and query $this['xxx'] = YourModel::where('slug',$this->param('yourslug'))->firstOrFail(); } catch (ModelNotFoundException $ex) { return Redirect::to('/404');
} }And then in your page:
{% for x in xxx %} {{ x.anyAttribute }} {% endfor %}
Hey it is working except for one thing, Pagination does not go to the next page. Would you know how to solve this?
$this['xxx'] = YourModel::where('slug',$this->param('yourslug'))->paginate(5);
Paginate
{% if xxx.LastPage > 1 %}
<ul class="pagination">
{% if xxx.CurrentPage > 1 %}
<li><a href="{{ this.page.baseFileName|page({ (pageParam): (xxx.CurrentPage-1) }) }}{{ this.page.query_string }}">← Prev</a></li>
{% endif %}
{% for page in 1..xxx.LastPage %}
<li class="{{ xxx.CurrentPage == page ? 'active' : null }}">
<a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}{{ this.page.query_string }}">{{ page }}</a>
</li>
{% endfor %}
{% if xxx.LastPage > xxx.CurrentPage %}
<li><a href="{{ this.page.baseFileName|page({ (pageParam): (xxx.CurrentPage+1) }) }}{{ this.page.query_string }}">Next →</a></li>
{% endif %}
</ul>
{% endif %}
I made this thread the other day on a similar issue I found
https://octobercms.com/forum/post/scope-pagination
1-3 of 3