imbayago imbayago - 1 year ago 125
JSON Question

Laravel search database table for partial match from query

I would like to write a search query that uses a supplied phrase to search a table and return the first value that matches any part of the phrase. Like using

to use to match.

The query i have needs to match the entire value otherwise it will always return null.

This is to check if an address exists in a database table.

$addressvalidation = CheckAddress::where('address', '=', $fulladdress)->value('address');

is the partial address like
1 Infinite Loop Cupertino
that i want to use to search in the database for a match.

is the query model for the table.

is the column name in the table.

The problem is that i can't use
to query the database with the partial phrase. The query gets a hit if i match the entire value like
1 Infinite Loop Cupertino, CA 95014

Answer Source

Try this:

$addressvalidation = CheckAddress::where('address', 'like', "%{$fulladdress}%")->value('address');

Also, checkout the documentation for where statements: