$students = Student::select('students.*', 'users.email')->join('users', 'students.user_id', 'users.id')->orderBy("id", "desc")->skip($page * $pageSize)->take($pageSize);
if (request('se') != "" || request('se') != null) {
$se = request('se');
$se = str_replace("+", " ", $se);
$students = $students
->where(function ($q) use ($se) {
$q->where('students.student_number', 'like', '%' .$se. '%')
->orWhere(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', '%' . $se . '%')
->orWhere('students.gender', $se)
->orWhere('students.phone', 'like', '%' .$se. '%')
->orWhere('email', 'like', '%' .$se. '%');
});
}
$students = $students->orderBy('id', 'DESC')->get();
when I search in the list I got Integrity constraint violation: 1052 Column 'first_name' in where clause is ambiguous error.
This got when join the table because both students and users table have first name and last name columns, but I have retrieved from only students table, I didn't understand why this appears again!
Are there any better solution for this?
Change the first_name and last_name to students.first_name and students.last_name respectively
Related
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
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();
I've 3 tables on my project:
Mall
Shop
Product
I've a page to search for products in the whole database. I need an option where they can search the product by the mall name. So I built my code like this:
$query = Product::with('shop', 'shop.mall');
if (!empty($data["keyword"])) {
$query = $query->where(function($q) use($data) {
$q->where('shop.mall.name', 'LIKE', '%' . $data["keyword"] . '%')->orWhere('shop.mall.keyword', 'LIKE', '%' . $data["keyword"] . '%');
});
}
But it is showing this error:
Column not found: 1054 Unknown column 'shop.mall.name' in 'where clause'
Is there any problem with my query? Thank you
To search within relation use whereHas(), it creates subquery and returns data filtered in shop.mall.
$query = Product::with('shop', 'shop.mall');
if (!empty($data["keyword"])) {
$query = $query->whereHas('shop.mall',function($q) uses ($data){
$q->where('name', 'LIKE', '%' . $data["keyword"] . '%')->
orWhere('keyword', 'LIKE', '%' . $data["keyword"] . '%');
});
}
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');