我使用的是基於Laravel的OctoberCMS。從組件向模型範圍傳遞變量
我想從URL獲取標識符,並將其傳遞給Scope來過濾數據庫結果。
$ this-> property('username')起作用並從URL返回用戶名。
但是,你如何將它傳遞給模型和範圍函數?
以下是Dynamic Scopes下的指南。
https://octobercms.com/docs/database/model#query-scopes
頁
URL:本地主機/用戶/無光澤
標識符:/用戶/:用戶名
結果組件
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()
解決方案?
我發現添加($查詢,$ 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屬性= $ _GET [ '用戶名'];而不是$ username = $ this-> property('username');你會知道真正的財產可以被訪問。 –
注意:也可以嘗試使用'Input :: get(「url_param」)' – Meysam
@Meysam它返回null。 –