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);
Related
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()
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.
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
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();
ENV:
Laravel 5.7.28
Database mysql
CentOS 7.2
I have 3 table like as below, I need join this 3 table and merge columns
(customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date)
to set 'like' query.
For example,
After coulms merge customer.first_name,customer.last_name,customer.address,job.name,job.address,job.date
customer.first_name + customer.last_name + customer.address + job.name + job.address +job.date
is
'TOMSMITHCecilia ChapmanABC.LtdIris Watson2019-01-10', so
when $text = 'MS';
set 'like' '%'.$text.'%' will return below result
customer.first_name = TOM
customer.last_name = SMITH
customer.address = Cecilia Chapman
job.name = ABC.Ltd
job.address = Iris Watson
job.date = 2019-01-10
id table (relation belongs To table customer and job)
id
customer_id
job_id
customer table
id
first_name
last_name
address
job id
id
name
address
job_date
See this
\DB::table('id')->join('customer','customer.customer_id','id.customer_id')
->join('job','job.id','id.job_id')
->where('customer.first_name', 'LIKE', '%' . $text . '%')
->orWhere('customer.last_name', 'LIKE', '%' . $text . '%')
->orWhere('customer.address', 'LIKE', '%' . $text . '%')
->orWhere('job.name', 'LIKE', '%' . $text . '%')
->orWhere('job.address', 'LIKE', '%' . $text . '%')
->get();
If you have multiple where condition then you may use where function with orWhere like this:
\DB::table('id')->join('customer','customer.customer_id','id.customer_id')
->join('job','job.id','id.job_id')
->where(function ($query) use($text) {
$query->where('customer.first_name', 'LIKE', '%' . $text .'%')
->orWhere('customer.last_name', 'LIKE', '%' . $text . '%')
->orWhere('customer.address', 'LIKE', '%' . $text . '%')
->orWhere('job.name', 'LIKE', '%' . $text . '%')
->orWhere('job.address', 'LIKE', '%' . $text . '%')
})->get();