laravel query builder search all fields - laravel

Guys I need to have a filter that validates in all fields with orWhere, for example: users? Search = Jhon he has to check that there is jhon in the name, email ....
Has anyone done this?
thankful

Same with other answer, you should use multiple orWhere() to do that
Here is the sample
$search = 'Jhon';
$conditions = ['name', 'email'];
$data = User::where(function ($query) use ($conditions, $search) {
foreach ($conditions as $column)
$query->orWhere($column, 'like', '%' . $search . '%');
})->get();

You Can get this type.
$query = User::query();
$columns = ['name', 'surname', 'address'];
foreach($columns as $column){
$query->orWhere($column, 'LIKE', '%' . $input . '%');
}
$users = $query->get();

Related

Search in all columns (Eloquent)

I am doing a method to search in all the columns of the table
$busqueda = $request->input('buscar');
/* Get columns */
$client = new Crmclient;
$table = $client->getTable();
$columns = \Schema::getColumnListing($table);
foreach ($columns as $column) {
$users_client = Crmclient::where('user_id', Auth::id())
->where($column, 'like', '%' . $busqueda . '%')
->orWhere('user_id_subaccount', Auth::id())
->where($column, 'like', '%' . $busqueda . '%')
->get();
}
whats wrong? I can get all the columns and if I do var_dump inside the loop in some interactions it looks for me and in others not, but finally I get the empty array
Try this
$columns = \Schema::getColumnListing((new Crmclient)->getTable());
$users_client = Crmclient::where(function ($query) {
$query->where('user_id', Auth::id())->orWhere('user_id_subaccount', Auth::id());
})->where(function ($query) use ($columns, $busqueda) {
foreach ($columns as $column) {
$query->orWhere($column, 'like', "%{$busqueda}%");
}
})->get();

How to search a term in eloquent relation of a model?

There is a Model named Profile that has user relation.
public function user(){
return $this->belongsTo(User::class);
}
and the profile table column is:
id | user_id | image | tel
The question is how I can search the name of a user from profile?
$term = request()->term;
$profiles = Profile::with('user')->where('name', 'like', '%'.$term.'%')->get();
Then I want to show the profiles that their name comes from the search term.
use whereHas() ref link https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence
$term = request()->term;
$profiles = Profile::whereHas('user',function($q) use($term){
$q->where('name', 'like', '%' . $term . '%');
})->get();
$filterUser = function ($q) use ($term) {
$q->where('name', 'like', '%' . $term. '%');
};
$term = request()->term;
$profiles = Profile::whereHas('user', $filterUser)->with(['user'=> $filterUser])->get();
If you want to syntactically optimize the code and want to use this more often then add this method in model
public function scopeWithAndWhereHas($query, $relation, $constraint){
return $query->whereHas($relation, $constraint)
->with([$relation => $constraint]);
}
And Usage:
$term = request()->term;
$profiles = Profile::withAndWhereHas('user', function($q) use ($term){
$q->where('name', 'like', '%' . $term. '%');
})->get();
You need to scope it on the subquery, something like this:
$profiles = Profile::with(['user' => function ($query) {
$query->where('name', 'like', '%'.$term.'%');
}])->get();

How To Get Search Query From Multiple Columns in Database

I have search form to get information from table named books.
Right now i'm using this controller
public function search(Request $request)
{
$keyword = $request->input('keyword');
$query = Book::where('judul', 'LIKE', '%' . $keyword . '%');
$book_list = $query->paginate(5);
$pagination = $book_list->appends($request->except('page'));
$total_book = $book_list->total();
return view('dashboards.index', compact('book_list', 'keyword', 'pagination', 'total_book'));
}
The problem is the data that i get from the request only available for judul. it just show empty result if the input keyword search addressed to search writter or publisher
I want the search form able to get data from other columns named writters and publisher
Is there any method to get data from multiple column?
You can use orwhere to fullfill this, like this
Book::where(function ($query) use($keyword) {
$query->where('judul', 'like', '%' . $keyword . '%')
->orWhere('writters', 'like', '%' . $keyword . '%');
})
->get();
I hope it helps you.
You can execute conditional queries in many ways.
1. You can use when():
Book::when($keyword, function ($q) use ($keyword) {
return $q->where('judul', 'LIKE', '%' . $keyword . '%');;
})
->get();
2. Use the where closure:
Book::where(function($q) use ($keyword, $request) {
if ($request) {
$q->where('judul', 'LIKE', '%' . $keyword . '%');
}
})
->get();
3. Do this:
$books = Book::query();
if ($request) {
$books = $books->where('judul', 'LIKE', '%' . $keyword . '%');
}
$books = $books->get();

query search LIKE use keywords where and left join LARAVEL 5

I want to find a data from two tables based on the name contained in the users, but I am having problems.
This is the users table containing the id, and name.
This is the student table that contains the id, and user_id.
Where user_id student = id users.
public function search(Request $request)
{
$keywords = trim($request->input('keywords'));
if (!empty($keywords)) {
$id_class = $request->input('id_class');
//Query
$query = User()
->leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
(!empty($id_class)) ? $query->Kelas($id_class) : '';
$data_student = $query->paginate(10);
//URL link pagination
$pagination = (!empty($id_class)) ? $pagination = $data_student->appends(['id_class' => $id_class]) : '';
$pagination = $data_student->appends(['keywords' => $keywords]);
$amount_data = $=data_student->total();
return view('student.index', compact('data_student', 'keywords', 'pagination', 'amount_data', 'id_class'));
}
return redirect('student');
}
Call to undefined function App\Http\Controllers\User()
As mentioned previously you need to correctly refer to the User class with namespace, you also need to change thew following:
$query = User()
->leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
to:
$query = User::leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
You must call the first method on the class as static with ::.
Also another potential typo you join the table student should it be plural; students?

Laravel eloquent search on fields of related model

I have an eloquent models as,
User : users(id, username, password, email, status)
Profile : profiles(id, user_id, first_name, last_name, gender, dob)
In the controller logic, I am eagerly loading the Profile model.
I can do this,
$user = User::with('Profile')->get();
or
$user = User::with('Profile')->where('status', '1')->get();
but how to do something like,
$user = User::with('Profile')->where('status', '1')->where('gender', 'Male')->get();
That's where whereHas comes in handy:
$user = User::with('Profile')->where('status', 1)->whereHas('Profile', function($q){
$q->where('gender', 'Male');
})->get();
Basically it adds the condition that the user needs to have a profile with gender = Male
If you want to search multiple columns in relation model.
$searchText = 'test text';
Product::with('owner')->where(function($query) use ($searchText)
{
$query->where('product_name', 'LIKE', '%' . $searchText . '%');
$columns = ['product_code', 'place_location', 'remark'];
foreach ($columns as $column ) {
$query->orWhere($column, 'LIKE', '%' . $searchText . '%');
}
$query->orWhereHas('owner', function($q) use ($searchText) {
$q->where(function($q) use ($searchText) {
$q->where('name', 'LIKE', '%' . $searchText . '%');
$q->orWhere('company_name', 'LIKE', '%' . $searchText . '%');
});
});
});
Let's say you've multiple relations
and you want to search records based on multiple relational columns value
User::with('associate')
->where('name', 'like', '%' . $input . '%')
->orWhere(function ($query) use ($input) {
$query->whereHas('associate', function ($q) use ($input) {
$q->where('first_name', 'like', '%' . $input . '%');
});
})
->orWhere(function ($query) use ($input) {
$query->with('roles')->whereHas('roles', function ($q) use ($input) {
$q->where('display_name', 'like', '%' . $input . '%');
});
})
->get();
Suppose, Your search input field name is q.
function name(Request $request){
$query = User::select();
if($request->q && $request->q !=''){
// if you search
$keyword = $request->q;
$query = User::with('Profile')->whereHas('Profile', function($q) use
($keyword){
$q->where('gender', 'like', "%{$keyword}%" );
});
}
$query->latest('id')->paginate();
}

Resources