I am trying to query my database that is connected to my model "Address" to find any addresses that are 'LIKE' any input into my form in the address input field. My model code is:
public function CheckForPrevious($verify)
{
$matches = \App\Address::where('address', 'like', '%'.$verify.'%')
->get();
foreach($matches as $match)
{
echo "$match";
}
}
My controller code is:
public function show()
{
$address = new Address;
$check = $_POST['address'];
$address->CheckForPrevious($check);
return $address;
}
Any help would be greatly appreciated
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";
}
}
use this
You don't have to prefix scope
for query scope functions
Change
`$address->scopeCheckForPrevious($check)`
To
`$address->CheckForPrevious($check)`
There is no need to access model unnecessary on the scope$query = App\Address::where('address', 'like', '%'.$verify.'%')
Try like this:
public function scopeCheckForPrevious($query, $verify)
{
return $query->where('address', 'like', '%'.$verify.'%');
}
And call it with the following code:
$address = new Address;
$check = $_POST['address'];
dd( $address->checkForPrevious($check)->get() );