我正在尝试查询连接到我的模型“Address”的数据库,以查找在地址输入字段中输入到我的表单中的任何地址。我的型号代码是:
public function CheckForPrevious($verify)
{
$matches = \App\Address::where('address', 'like', '%'.$verify.'%')
->get();
foreach($matches as $match)
{
echo "$match";
}
}
我的控制器代码是:
public function show()
{
$address = new Address;
$check = $_POST['address'];
$address->CheckForPrevious($check);
return $address;
}
非常感谢任何帮助
答案 0 :(得分:1)
public function show()
{
$address = new Address();
$check = $_POST['address'];
$address->scopeCheckForPrevious($check);
}
public function scopeCheckForPrevious($ verify)
{
$query = App\Address::where('address', 'like', '%'.$verify.'%')
->get();
foreach($query as $match)
{
echo "$match";
}
}
使用此
答案 1 :(得分:0)
您不必在scope
前加上查询范围函数
更改
`$address->scopeCheckForPrevious($check)`
要
`$address->CheckForPrevious($check)`
答案 2 :(得分:0)
无需访问范围上不必要的模型
$query = App\Address::where('address', 'like', '%'.$verify.'%')
试试这样:
public function scopeCheckForPrevious($query, $verify)
{
return $query->where('address', 'like', '%'.$verify.'%');
}
并使用以下代码调用它:
$address = new Address;
$check = $_POST['address'];
dd( $address->checkForPrevious($check)->get() );