0

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

3
  • You are not using scope correctly, take a look on the documentation laravel.com/docs/5.4/eloquent#local-scopes Commented May 20, 2017 at 5:26
  • @jogesh_pi I thought I did use scope right I have the instance used at the top of my model and I used the word scope before the name of my method. I don't know what I am missing Commented May 20, 2017 at 5:31
  • Here is my latest code: public function CheckForPrevious($verify) { $matches = \App\Address::where('address', 'like', '%'.$verify.'%') ->get(); ` foreach($matches as $match) { echo "$match"; } }I get a return of an empty array Commented May 20, 2017 at 5:32

3 Answers 3

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



    }

use this

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have to prefix scope for query scope functions

Change

  `$address->scopeCheckForPrevious($check)`

To

  `$address->CheckForPrevious($check)`

Comments

0

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

1 Comment

There are two parameters in the first Model portion of the code and there is only one in the controller

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.