Multiple search clause for many-to-many relationships in Laravel - php

Good day. I have this stuff that has been a little bit problematic for me in my application. I have a Staff model which has a many to many relationship with a Company model. Hence, I have a company_staff table.So, the companies have a unique identifier which is the company code.
The Staff model has information such as first name, lastname, and email etc. Now, I want to perform a search operation using either the firstname, lastname or email, but fetching data for only the company with the unique code (i.e search only within that company). This is the query I came up with below:
$users = Staff::where('first_name', 'like', '%' . $request->search_term . '%')
->orWhere('last_name', 'like', '%' . $request->search_term . '%')
->orWhere('email', 'like', '%' . $request->search_term . '%')
->whereHas('coporate', function ($q) use ($company_code) {
$q->where('company_code', $company_code);
})->get();
I get results but this only returns all the result that match from my "staff" table, behaving as if the "company_code" part of the query does not exist at all. However, if I leave the query as :
$users = Staff::where('first_name', 'like', '%' . $request->search_term . '%')
->whereHas('coporate', function ($q) use ($company_code) {
$q->where('company_code', $company_code);
})->get();
I get the desired result. But, I want to be able to search with both last_name and email as well. Please, how do I go about this?

Try with this code
$users = Staff::where(function ($query) use($request) {
$query->where('first_name', 'like', '%' . $request->search_term . '%')
->orWhere('last_name', 'like', '%' . $request->search_term . '%')
->orWhere('email', 'like', '%' . $request->search_term . '%');
})->whereHas('coporate', function ($q) use ($company_code) {
$q->where('company_code', $company_code);
})->get();
For more : https://laravel.com/docs/6.x/queries#parameter-grouping

Related

like and whereIn query doesn't show result as expected

this query still shows the result of the like query even if the item on the like query doesn't exist on the tag_id
User::where('name', 'like', '%' . request()->search . '%')
->orWhere('description', 'like', '%' . request()->search . '%')
->whereIn('tag_id', [1, 3])
->get()
how do I show the result that exists only in the tag_id ?
so for example, if the like query finds John but John is not in the tag_id of 1,3, it won't show the result of like John
I'm not familiar with lavarel but it should work
According to the documentation, you can pass function query builder in a where clause.
It allow you to do a OR between your like conditions only, and a AND between this part and the whereIn
where (A or B) and C
User::where(function($query) {
$query->where('name', 'like', '%' . request()->search . '%')
->orWhere('description', 'like', '%' . request()->search . '%');
})
->whereIn('tag_id', [1, 3])
->get()

Laravel Query is not working as aspected on multiple where and orWhere?

I have a data having table name users that has fields of id, role_id, first_name, last_name, name, and email. What I trying to get is the role_id of 2 with any match from the name, first_name, or last_name.
Laravel Framework 8.77.1
PHP 7.4.3
The database table is
What I am trying is the Laravel Query
public function search_friend_custom(Request $request) {
$search = $request->input('search');
$search = 'chase ';
// ** $Search variable has chase string for searching.**
$friends = DB::table('users')
->select('users.*','users.id as user_tab_id')
->where('role_id', 2)
->where('name', 'like', '%' . $search . '%')
->orWhere('first_name', 'like', '%' . $search . '%')
->orWhere('last_name', 'like', '%' . $search . '%')
->paginate(10);
return view('user.custom_search' , compact('friends'));
}
What it is returing are both Chase's Hadyman Services with role_id=3 and Chase Mary role_id=2 both. But the aspected result is only Chase Mary as it's role_id is 2.
Please guide, I am not getting what I am missing here?
You should logically group them inside a closure so your role_id takes precedence but doesn't interfere with the rest of the query.
$friends = DB::table('users')
->select('users.*','users.id as user_tab_id')
->where('role_id', 2)
->where(fn($builder) => $builder->where('name', 'like', '%' . $search . '%')
->orWhere('first_name', 'like', '%' . $search . '%')
->orWhere('last_name', 'like', '%' . $search . '%')
)->paginate(10);
See the documentation for more references.

how to convert this query in eloquent?

I am a beginner laravel developer who wants to convert this query into an eloquent ORM. I know eloquent make it small. I search a lot but was always stuck in where condition It working perfectly but need to change in eloquent.
Product::join('subcategories', 'subcategories.id', '=',
'products.subcategory_id')
->join('categories', 'categories.id', '=', 'products.category_id')
->WHERE('products.name', 'like', '%' . $search . '%')
->orWHERE('subcategories.name', 'like', '%' . $search . '%')
->orWHERE('categories.name', 'like', '%' . $search . '%')
->orWHERE('products.description', 'like', '%' . $search . '%')
->orWHERE('products.price','=', $search)
->select('products.*')
->paginate(5);
In the second query, I want to select User where the role is admin but id not equal to 1.
User::where(['role'=>admin, 'id',!=,1])->first();
I am using this please guide me
User::where('role',admin)->where('id',!=,1)->first();

Laravel Query builder, how to have 1 leading where and the other orwhere?

I'm having a problem with my query.
I want to filter through all Orders where user_id is the authenticated user. And run the searchquery on field number1 and field number2.
Query:
$query = Order::query();
$query->where('user_id', Auth()->id())
->where('number','LIKE', '%' . $searchquery . '%')
->orWhere('number2,'LIKE', '%' . $searchquery . '%')
->with('user');
I want to ALWAYS check where user_id is Auth()->id();
But when i run this orWhere it also gets orders from other users.
Thanks in advance.
Do it like this :
$query = Order::query();
$query->where('user_id', Auth()->id())
->where(function ($query) use ($searchquery) {
$query->where('number','LIKE', '%' . $searchquery . '%');
$query->orWhere('number2,'LIKE', '%' . $searchquery . '%');
})
->with('user');

Laravel - Querying related model

Basically I am trying to execute the following query.
Volunteer::with('user')
->where('fname', 'like', '%' . Input::get('search') . '%')
->orWhere('lname', 'like', '%' . Input::get('search') . '%')
->orWhere('users.email', 'like', '%' . Input::get('search') . '%') //error
->orWhere('phone', 'like', '%' . Input::get('search') . '%')
->orderBy('fname')
->paginate($limit);
The second onWhere clause throws column not found error.
volunteers and users table are related.
How do I use email column from users table while querying volunteers table like shown above.
Please help. Any workaround?
What you are trying to do is query the relationship. For this example it can be accomplished like this:
Volunteer::with('user')
->where('fname', 'like', '%' . Input::get('search') . '%')
->orWhere('lname', 'like', '%' . Input::get('search') . '%')
->orWhereHas('user', function($query){
$query->where('email', 'like', '%' . Input::get('search') . '%');
})
->orWhere('phone', 'like', '%' . Input::get('search') . '%')
->orderBy('fname')
->paginate($limit);

Categories