I got a problem when search query using Laravel where and like.
$words = 'pa';
$query = Category::where(function ($query) use ($words) {
$query->where('name', 'like', '%'.$words)
->orWhere('name', 'like', $words . '%')
})->pluck('name');
The result is:
[{Chocolate Spa, Zen Spa, Disco Party}]
The expected result is only Party .
I want it search name column starts with the given word not contain that words.
How can I achieve that search method?
This should work:
$query = Category::where('name', 'like', $words.'%')
->orWhere('name', 'like', '% '.$words.'%'))
->pluck('name');
Related
My Code;
$posts=Term::where('user_id',$user_id)->where('status',1)->where('type','product')->with('preview','attributes','category','price','options','stock','affiliate')->withCount('reviews');
if(!empty($request->term)){
$data= $posts->where('title','LIKE','%'.$request->term.'%');
}
This my code searches for title from term table. But I want to search from another table without breaking the structure. So Example;
if(!empty($request->term)){
$data= $termTABLE->where('title','LIKE','%'.$request->term.'%');
$data= $stockTABLE->where('code','LIKE','%'.$request->term.'%');
}
Since I don't know about Laravel, I couldn't explain it fully. I hope I get help. Thanks.
Good Luck
The below example is not like yours' but you can sort it out according to your need.
For example, if user has a company you can search in the company table like
$user->where('name', 'LIKE', '%' . $searchPhrase . '%') //Searching in user table
//Searching in user table
->orWhere('email', 'LIKE', '%' . $searchPhrase . '%')
//Checking if company exist
->orWhereHas('company', function ($query) use ($searchPhrase) {
//Searching in company table
$query->where('name', 'LIKE', '%' . $searchPhrase . '%');
});
I'm trying to make a search feature on laravel. now I'm using this query:
$products = Product::with(['category', 'store'])
->when($keywords, function ($query) use ($keywords) {
$query->where('name', 'LIKE', '%' . $keywords . "%")
->orWhere('description', 'LIKE', '%' . $keywords . '%');
})->get()
The problem is, say on the database i've a product with the name corrupti, If i searched by the exact name or removed a character from beginning or end it works fine, but if I changed a single character it returns an empty list.
Users are likely to make typos, so I want to be able to find the product is the user typed corrupta, corrupi instead of corrupti.
I know this is not a simple task to achive, I googled many things but I didn't find a solution.
One thing I've came accross is the php similar_text funciton, it may be useful but I didn't find a way to include it in the database query.
https://www.php.net/manual/en/function.soundex.php
https://www.php.net/manual/en/function.metaphone.php
use metaphone (or soundex) to encode words you want to be searchable
put them in a database column say products.name_as_metaphone
make your search function encode searched word to metaphone, then make it look in the metaphone column (and not in product.name)...
profit
try this:
$query = Product::query();
$searches = explode(' ', $request->query('q'));
if (isset($searches)) {
foreach ($searches as $search) {
$query->where(function ($q) use ($search) {
$q->where('code', 'ilike', '%' . $search . '%')
->orWhere('name', 'ilike', '%' . $search . '%');
});
}
}
$data = $query->get();
return ([
'success' => true,
'response' => $data,
'message' => 'Your message here'
]);
I have a query like so
$data = City::with('hotel')->orwherehas('hotel', function ($query) use ($user_input) {
//here i want to limit this result to 5
$query->where('name', 'LIKE', '%' . $user_input . '%')->take(5);
// $query->take(5); i have tried this too
})->orWhere('name', 'LIKE', '%' . $user_input . '%')->get();
inside the whereHas clause, I have a query that I want to limit to 5, now I tried limit, take but no luck after that where nothing is working I don't know why
You can pass your query to the ->with() query builder method:
$data = City::with(['hotel' => function($query) use ($user_input) {
$query->where('name', 'LIKE', '%' . $user_input . '%')->limit(5);
}])
->where('name', 'LIKE', '%' . $user_input . '%')
->get();
This will get all hotels associated with a city which have the user input, where the city contains the user input.
Note that the ->orWhere() is not used here.
I have this function in my controller:
if ($request->get('query')) {
$query = $request->get('query');
$data = DB::table('users')
->where('is_archive', '=', 0)
->where(function($string) use ($query) {
$string->where('first_name', 'LIKE', '%' . $query . '%')
->orWhere('last_name', 'LIKE', '%' . $query . '%')
->orWhere('church_id', 'LIKE', '%' . $query . '%');
})
->get();
}
What it does is a real-time search result that is listing same exact results as you type a letter. Same as the search bar of google that every time when you type a letter. it will show results that is the same as the currently typed text. I wish to put security features that will prevent the rendering of <, >, ', " etc. but it will accept space (spacebar) character. What I have done so far is:
$sanitizedQuery = strtolower(trim(preg_replace('/\s+/', '', $query)));
but I seem to fail to achieve my desired outcome. Any suggestion is highly appreciated.
I have created a simple search function that works by searching my required fields to match my keywords. This works fine for every column apart from my "Id" column, because when I display my ID in the table I actually add + 500 and some text. This is basically because the ID column is my booking/order reference and I didn't want to start at 0 and felt it easier to just offset the displayed result.
Basically I need it to add 500 to the search if when checking against the request_id column. So for the first record in the table would be 1 + 500 + my prefix which displays as SP-501. It I search for 501, it actually brings up record 501 in the table and not the correct one.
Heres my code:
public function search()
{
$search = \Request::get('search');
$bookings = LcsBooking::where('request_email','like','%'.$search.'%')
->orWhere('request_id', 'like', '%'.$search.'%')
->orWhere('request_name', 'like', '%'.$search.'%')
->orWhere('request_arrival', 'like', '%'.$search.'%')
->orWhere('request_return', 'like', '%'.$search.'%')
->orWhere('request_txauthno', 'like', '%'.$search.'%')
->orWhere('request_vehicle_reg', 'like', '%'.$search.'%')
->orWhere('request_mobile', 'like', '%'.$search.'%')
->orderBy('request_id', 'DESC')
->paginate(20);
return view('admin.lcs.search',compact('bookings'));
}
You could just make a check before to see if it's a string integer, and if so, add 500 to it.
$search = \Request::get('search');
$id = ctype_digit($search) ? $search + 500 : $search;
$bookings = LcsBooking::where('request_email','like','%'.$search.'%')
->orWhere('request_id', 'like', '%'.$id.'%')
->orWhere('request_name', 'like', '%'.$search.'%')
// and so on...