3

I'm using OctoberCMS based on Laravel.

I'm trying to get the identifier from URL and pass it to the Scope to filter database results.

$this->property('username') works and returns the username from the URL.

But how do you pass it to the Model and into the Scope function?

Here is the guide, under Dynamic Scopes.

https://octobercms.com/docs/database/model#query-scopes

Page

URL: localhost/user/matt

Identifier: /user/:username

Results Component

public function init()
{
    // get username from url
    $username = $this->property('username'); //matt

    // pass username to scope
    Gallery::applyUser($username);
}

Gallery Model

// return results that match username
public function scopeApplyUser($query, $username)
{
    return $query->where('username', $username);
}

Error

Missing argument 2 for MyVendor\Gallery\Models\Gallery::scopeApplyUser()

Solution?

I found adding ($query, $username = null) allows the variable to pass without error.

But now the problem is that $username is both 'matt' and null at the same time and never makes it to the query return.

// return results that match username
public function scopeApplyUser($query, $username = null)
{
    print $username; //prints matt
    if ($username == null) { print 'null'; } //prints null

    return $query->where('username', $username); //returns null
}

diagram

3
  • Try to get property from url using as below $username = $_GET['username']; instead of $username = $this->property('username'); you will come to know really property can be accessed. Commented Feb 20, 2017 at 8:08
  • Side note: Also try using Input::get("url_param") Commented Feb 21, 2017 at 5:34
  • @Meysam It returned null. Commented Feb 21, 2017 at 22:57

1 Answer 1

1

In a model gallery you need a field user:

class Gallery extends Model {
    /** these are just placeholder variables you should be recognising these from your own model.!**/
    protected $table = 'gallery';
    protected $guarded = ['*'];
    protected $fillable = ['username'];// Make sure this also is in the database!!
    public scopeWhereUser($query, $user = null) {
        if(!is_null($user)) { // Only do something if the username is provided!
           $query->where('username',$user);
        }
    }
}

Then, when you have the nullchecks you can just call it with

 $gallery = new Gallery();
 $query = $gallery->newQuery()->where('x','y')->whereUser('matt');

Changes I made:

  • renamed scope to whereuser instead of apply user because it's more logical to name it that way. Apply you do to something functional that changes a state permanently
  • added a not is_null() check to only limit the query when it's not null.

full working example that prints the results:

 $gallery = new Gallery();
 $query = $gallery->newQuery()->where('x','y')->whereUser('matt');
 $results = $query->get();
 foreach($results as $galleryItem) {
     echo $galleryItem->getKey() . ' is having:<BR/>';
     echo dump([
               'model'=>$galleryItem,
               'methods' => get_class_methods(get_class($galleryItem)),
               ]);
 }
Sign up to request clarification or add additional context in comments.

9 Comments

The !is_null shows $username is not null, but I got Parse error: syntax error, unexpected '$table' (T_VARIABLE), expecting function (T_FUNCTION). and the $query gave 502 Bad Gateway.
They are just placeholders.. you're only supposed to inject the scope function into your model.
I've put the scope in my model, then pass a variable to it. It works, the variable prints, but when I return it in the query it's null. How can it have value and be null at the same time?
it's not. You're not understanding how the query builder works. Let me update it for you.
I dont understand where to use $gallery = new Gallery(). In OctoberCMS, you drag and drop the Record List and choose your Scope function from the one in your Model. Then you pass a $type to the Scope, and inside the Scope you return the $query. Now the Record List is filtered by that Scope. I'm passing it from a Component.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.