I am very new in Laravel, I try to search with one column and this is my code. But now I want to search with multiple columns - how can I do that?
My controller :
public function search_pers(Request $request)
{
$mle = $request->input('mle');
$listpers = Personne::where('mle', 'LIKE', '%'.$mle.'%')->get();
return view('frontend.etat_civil', ['personnes' => $listpers]);
}
You could use an array
Personne::where([
['col1', '=', 'value1'],
['col2', '<>', 'value2'],
[Your_col, OPERATOR, value],
...
])
or in your case
$str = "concat('%',".$mle .",'%')";
$listpers = Personne::where([
['mle', 'LIKE', $str],
['col1', '=', 'value1'],
['col2', '<>', 'value2'],
[Your_col, OPERATOR, value],
)->get();
As I understand what you need is an orWhere condition:
public function search_pers(Request $request)
{
$mle = $request->input('mle');
$listpers = Personne::where('mle', 'LIKE', '%'.$mle.'%')->orWhere('nom','like','%'.$mle.'%')->get();
return view('frontend.etat_civil', ['personnes' => $listpers]);
}
Another alternative and probably a better one is to use fulltext search.
MySQL Fulltext search
Related
I am writing a query using with and then where clause in laravel.
$users = User::query()->with('roles')->where('name', '!=', 'customer')->get();
return $users;
But where clause is not working here. Customers are not excluded. I am providing the snap shot of the query result.
I think you need whereHas() :
use Illuminate\Database\Eloquent\Builder;
$users = User::query()
->with('roles')
->whereHas('roles', function (Builder $query) {
$query->where('name', '!=', 'customer');
})
->get();
return $users;
I suppose you trying to filter relation data in base query.
Try smth like that:
$users = User::query()->with([
'roles' => function ($q) {
$q->where('name', '!=', 'customer');
}])
->get();
return $users;
Doc
I'm trying to do a search using where in po website.
My code:
public function globalSearch($subject) {
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
$results = array();
foreach ($users as $user) {
$results[$user->username] = url('player/'.$user->slug);
}
return $results;
}
But it's only searching for steam_id and if I put username it does not find anything. I think the problem is, that I need to use multiple wheres in 1 line. Sorry if you don't understand what I mean but how can I use 1 where with multiple fields?
Below two lines are overriding your search.So remove that two lines and write it as below.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
User below code.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->orwhere('steam_id', 'LIKE', '%'.$subject.'%')->get();
You're overwriting $users with that second statement. If you want a result with both where clauses, you can use an array as options:
$users = User::where([
['username', 'LIKE', '%'.$subject.'%'],
['steam_id', 'LIKE', '%'.$subject.'%']
])->get();
More on Laravel where clauses.
Will this do the job?
public function globalSearch($subject)
{
return User::where('username', 'LIKE', '%'.$subject.'%')->orWhere('steam_id', 'LIKE', '%'.$subject.'%')->get();
}
Ideally group by the table index to not get unexpected results.
You can either use an array inside of the User::where() like #kerbholz answers states correctly. Or you can go for a chaining like so:
User::where('foo', 1)->where('bar', false)->get();
Which will result in the same result as the answer above but in my opinion is usually more readable.
My goal is to search for a first and last name using Laravel. As the code is written, it searches for a First Name or Last Name, what I would like to do is get it to search for both First Name and Last Name. How would I accomplish this?
PHP - Search Statement
public function search($search_input) {
$response = DB::table(self::$table)
->select(array(
'ID',
'User_ID',
'Signup_Date',
'First_Name',
'Last_Name',
DB::raw('CONCAT(First_Name," ",Last_Name) AS Name')
))
->where('First_Name', 'LIKE', '%'.$search_input.'%')
->or_where('Last_Name', 'LIKE', '%'.$search_input.'%')
->where_not_null('User_ID')
->order_by('Signup_Date', 'desc')
->get();
return $response;
}
EDIT: I forgot to include we are using Laravel 3. https://laravel3.veliovgroup.com/docs/database/fluent
As I understand, your query condition is
(First_Name LIKE "%xx%" OR Last_name LIKE "%xx%") AND User_id IS NOT NULL
You should modify your query as below to use nested where clauses:
public function search($search_input) {
$response = DB::table(self::$table)
->select(array(
'ID',
'User_ID',
'Signup_Date',
'First_Name',
'Last_Name',
DB::raw('CONCAT(First_Name," ",Last_Name) AS Name')
))
->where(function($query) use ($search_input) {
$query->where('First_Name', 'LIKE', '%'.$search_input.'%')
->or_where('Last_Name', 'LIKE', '%'.$search_input.'%');
}
->where_not_null('User_ID')
->order_by('Signup_Date', 'desc')
->get();
return $response;
}
If you have it search both first name and last name, then I doubt it would return results. Because it would imply the search query would have to match a record where the input is like the first name and like the last name. I think what you are actually after is more of a compound search. As you have the selects in your query, you might be able to do the following instead.
public function search($search_input) {
$response = DB::table(self::$table)
->select(array(
'ID',
'User_ID',
'Signup_Date',
'First_Name',
'Last_Name',
DB::raw('CONCAT(First_Name," ",Last_Name) AS Name')
))
->where('Name', 'LIKE', '%'.$search_input.'%')
->where_not_null('User_ID')
->order_by('Signup_Date', 'desc')
->get();
return $response;
}
This should work because Name is being set by the CONCAT function.
I have a project which is creating in laravel and mongodb. I am using a package for connecting to mongodb "https://github.com/jenssegers/laravel-mongodb". I have a table listing. In this table listing there is a text box for search data. For that I used like feature. I have followed the same in documentation but not working. When I search invoice_number data is getting empty. But when I search beds data is getting perfectly. Please correct me.
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%")
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
The WHERE statements in your query translate to this:
WHERE [cond_1] AND [cond_2] OR [cond_3]
I doubt this is the condition you need. I'd say you want this:
WHERE [cond_1] AND ([cond_2] OR [cond_3]) -- notice the brackets.
To achieve this, you need a closure in your Builder query. Try this:
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where(function($query) use ($search) { /* That's the closure */
$query->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%");
})
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
And an example from the docs.
I am stuck with search functionality from two different tables (VenueHall and Venue). The data is coming in the search1 parameter. I am able to search the data from one table (VenueHall) but I am not able to map with another table (Venue) using the existing object from the first table ($VenueHall`).
$VenueHall = VenueHall::orderBy('created_at', 'DESC');
$VenueHall = $VenueHall->with('venue');
$VenueHall = $VenueHall->with('venueType');
$VenueHall = $VenueHall->with('venue.area');
if (Input::has('query') && $searchQuery = Input::get('query'))
{
$perecentilify = '%'. $searchQuery .'%';
$VenueHall = $VenueHall->where(function($query) use ($perecentilify, $searchQuery) {
$query->where('name', 'LIKE', $perecentilify)
->orWhere('code', 'LIKE', $perecentilify);
//->orWhere('from venue table');
});
}
$data = array(
'venueHall' => $VenueHall->paginate(2)
);
return View::make('venues/venues', array('data' => $data));
I am not able to map the //->orWhere('from venue table') clause which comes from the Venue table. Please help me out get the data from the two tables based on the search value in the search box.
Often I find joins to be the easiest solution in such cases:
$VenueHall = VenueHall
::join('Venue', 'Venue.id', '=', 'VenueHall.venue_id')
->where('VenueHall.name', 'LIKE', $perecentilify)
->orWhere('VenueHall.code', 'LIKE', $perecentilify)
->orWhere('Venue.name', 'LIKE', $perecentilify)
->orderBy('created_at', 'DESC')
->with(['venue', 'venueType', 'venue.area'])
->paginate(2)
;