【问题标题】:Pass Variable from Component to Model Scope将变量从组件传递到模型范围
【发布时间】:2017-07-09 07:53:25
【问题描述】:

我正在使用基于 Laravel 的 OctoberCMS。

我正在尝试从 URL 中获取标识符并将其传递给 Scope 以过滤数据库结果。

$this->property('username') 有效并从 URL 返回用户名。

但是如何将它传递给 Model 和 Scope 函数呢?

这是动态范围下的指南。

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

页面

网址:localhost/user/matt

标识符:/user/:用户名

结果组件

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

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

画廊模型

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

错误

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

解决方案?

我发现添加 ($query, $username = null) 允许变量无错误地传递。

但现在的问题是 $username 同时是 'matt' 和 null 并且永远不会返回查询。

// 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
}

【问题讨论】:

  • 尝试使用如下方式从 url 获取属性 $username = $_GET['username'];而不是 $username = $this->property('username');你会知道真正的财产是可以访问的。
  • 旁注:也可以尝试使用Input::get("url_param")
  • @Meysam 它返回 null。

标签: php laravel laravel-5 octobercms


【解决方案1】:

在模型库中,您需要一个现场用户:

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);
        }
    }
}

然后,当你有空检查时,你可以调用它

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

我所做的更改:

  • 将范围重命名为 whereuser 而不是 apply user,因为这样命名更合乎逻辑。将您所做的事情应用于永久改变状态的功能
  • 添加了 not is_null() 检查以仅在查询不为空时限制查询。

打印结果的完整工作示例:

 $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)),
               ]);
 }

【讨论】:

  • !is_null 显示 $username 不为空,但我收到 Parse 错误:语法错误,意外的 '$table' (T_VARIABLE),期望函数 (T_FUNCTION)。并且 $query 给出了 502 Bad Gateway。
  • 它们只是占位符......你只应该将范围函数注入你的模型中。
  • 我已将范围放入我的模型中,然后将变量传递给它。它有效,变量打印,但是当我在查询中返回它时它为空。怎么可能同时有值又为空?
  • 不是。您不了解查询生成器的工作原理。让我为你更新它。
  • 我不明白在哪里使用 $gallery = new Gallery()。在 OctoberCMS 中,您拖放记录列表并从您的模型中选择您的 Scope 函数。然后将 $type 传递给 Scope,并在 Scope 内返回 $query。现在记录列表由该范围过滤。我从一个组件传递它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多