Search in multiple tables by eloquent query - php

I have two tables: posts and pages. Based on the keyword, I want to search different columns of the two tables for the occurrence of keyword. The two tables do not have related content.
I have written two queries one for each table. Following are the queries:
$result1 = Post::where('title_en', 'LIKE', '%' . $keyword . '%')
->or_where('title_np', 'LIKE', '%' . $keyword . '%')
->order_by('id', 'desc')
->paginate(10);
$result2 = Page::where('title', 'LIKE', '%' . $keyword . '%')
->or_where('content', 'LIKE', '%' . $keyword . '%')
->order_by('id', 'desc')
->paginate(10);
The above queries return two different Laravel\Paginator object. But I want a single Laravel\Paginator object so that a single pagination is displayed on the page which works for both the queries or on a single query which achieves the functionality of both the above queries. How would I be able to do that?

Since they are unrelated tables, you need to do some trickery that's simply too complex for Eloquent to handle, you need to do something similar to the following to join the two queries together, and still be able to do your limit / ordering on the combined queries:
$results = DB::query('SELECT id, title FROM (
SELECT id, title FROM `post` where `title_np` LIKE "%'.$keyword.'%" OR `title_en` LIKE "%'.$keyword.'%"
UNION ALL
SELECT id, title FROM `page` where `title` LIKE "%'.$keyword.'%" OR `content` LIKE "%'.$keyword.'%"
) temp_table
ORDER BY `id` desc
LIMIT 0, 10
')
NB the above is untested, purely an example of the approach you'll need to take, not sure this will actually work.

I know this question has been answered ages ago but If someone ends up here like me, I had a very similar issue, and here is how I did it using the eloquent way :
$users = User::where('active', 1)
->where(function ($subQuery) use ($searchKeyword) {
$subQuery->where('pseudo', 'LIKE', "%$searchKeyword%")
->orWhere('first_name', 'LIKE', "%$searchKeyword%")
->orWhere('last_name', 'LIKE', "%$searchKeyword%")
->orWhere('email', 'LIKE', "%$searchKeyword%");
})
->whereHas('addressPrincipal')
->select(array_merge(['id', 'pseudo', DB::raw("CONCAT(users.first_name,' ',users.last_name) as name")],
[DB::raw('"user" as source')]));
$usersAll = Subsidiary::where('active', 1)
->where(function ($subQuery) use ($searchKeyword) {
$subQuery->where('slug', 'LIKE', "%$searchKeyword%")
->orWhere('name', 'LIKE', "%$searchKeyword%")
->orWhere('email_principal', 'LIKE', "%$searchKeyword%");
})
->unionAll($users)
->select(array_merge(['id', 'slug as pseduo', 'name'],
[DB::raw('"subsidiary" as source')]))
->paginate(10);
You must have noticed that I have used array_merge in the select statement, I have done this as I needed to know from which table respective records were getting fetched.

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 From RAW Query to Eloquent Query

I'm trying to convert my Raw query to an eloquent query
You can see here
$data = DB::connection('mysql')->select("SELECT product.id, product.title, product.description, product.content, product.photo, product_per_category.productcategory_id, product.quantity, product.price
FROM product LEFT JOIN product_per_category ON product_per_category.product_id = product.id
WHERE product.deleted = 0 AND product_per_category.deleted = 0 AND productcategory_id = '$id' AND (product.title like '%$keyword%' OR product.content like '%$keyword%' OR product.price like '%$keyword%' OR product.quantity like '%$keyword%')
GROUP BY product.id");
I have multiple WHERE statement that combines with AND & OR inside of a parentheses
I just want to know if Im doing this right with my eloquent query like this
$data = DB::table('product')
->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')
->join('product_per_category','product_per_category.product_id','=','product.id')
->where(['product.deleted' => 0])
->where(['product_per_category.deleted' => 0])
->where(['product_per_category.productcategory_id' => $id])
->orWhere('product.content', 'like', '%' . $keyword . '%')
->orWhere('product.title', 'like', '%' . $keyword . '%')
->orWhere('product.quantity', 'like', '%' . $keyword . '%')
->orWhere('product.price', 'like', '%' . $keyword . '%')
->groupBy('product.id')
->get();
Because I wonder in my query I have OR statement inside of a parentheses.
Which I combine them inside of the parentheses to make it only the fields optional with LIKE statement
You're pretty close, but when you need to have conditions between parenthesis, your where() functions should be a callback.
For example (product.title like '%$keyword%' OR product.content like '%$keyword%' OR product.price like '%$keyword%' OR product.quantity like '%$keyword%')
would be
$query->where(function($subquery) use($keyword) {
$subquery->where('title', 'like', "%{$keyword}%")
->orWhere('content', 'like', "%{$keyword}%");
});
This is only a rough example of your requirement, but you should get it.
Just so you know, you can combine almost ALL Eloquent functions.
Good luck!
You can pass a closure inside the where method.
The closure will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis.
This is known as parameter grouping.
https://laravel.com/docs/7.x/queries#parameter-grouping
Change your statement to this.
$data = DB::table('product')
->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')
->join('product_per_category','product_per_category.product_id','=','product.id')
->where(['product.deleted' => 0])
->where(['product_per_category.deleted' => 0])
->where(['product_per_category.productcategory_id' => $id])
->where(function($query) use($keyword){
$query->where('product.content', 'like', '%' . $keyword . '%')
->orWhere('product.title', 'like', '%' . $keyword . '%')
->orWhere('product.quantity', 'like', '%' . $keyword . '%')
->orWhere('product.price', 'like', '%' . $keyword . '%');
})
->groupBy('product.id')
->get();

Eloquent WHERE LIKE clause with more than two columns

I've been trying to do a query in Laravel that in raw SQL will be like this
"SELECT * FROM students WHERE (((students.user_id)=$id) AND (((students.name) Like '%$q%') OR ((students.last_name) Like '%$q%') OR ((students.email) Like '%$q%')))")
I follow this thread (Eloquent WHERE LIKE clause with multiple columns) and it worked fine, but only with two columns Ej:
$students = student::where(user_id, Auth::id())
->whereRaw('concat(name," ",last_name) like ?', "%{$q}%")
->paginate(9);
But if I add more than two columns then the resultant variable is always empty, no matter if what is in the variable $q match with one or more columns:
$students = student::where(user_id, Auth::id())
->whereRaw('concat(name," ",last_name," ",email) like ?', "%{$q}%")
->paginate(9)
I am pretty sure I am missing something but i can't find what it is. Thanks in advance.
You can do something like this:
$students = student::where('user_id', Auth::id())->where(function($query) use ($q) {
$query->where('name', 'LIKE', '%'.$q.'%')
->orWhere('last_name', 'LIKE', '%'.$q.'%')
->orWhere('email', 'LIKE', '%'.$q.'%');
})->paginate(9);
The above Eloquent will output SQL similar to
"SELECT * FROM students WHERE students.user_id = $id AND (students.name like '%$q%' OR students.last_name Like '%$q%' OR students.email Like '%$q%')"

Eloquent grouped orWhere

Currently have an Eloquent statement:
$contacts = Contacts::where('lname','LIKE',$searchquery.'%')
->orWhere('fname','LIKE',$searchquery.'%')
->orWhere('phone','LIKE','%'.$searchquery)
->where('active','=',1)->get();
It is treating it as
select
*
from
contacts
where
lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%'
and active = 1
what I am needing is
select
*
from
contacts
where
(lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%')
and active = 1
How do I go about grouping in Eloquent? I have found a couple examples such as:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
But I am only used to Eloquent, not Laravels DB Query builder. I tried adapting the Eloquent form to this
$contacts = Contacts::->where('active', '=', '1')
->where(function ($query) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->get();
No success as it does not recognize the $searchquery inside the function.
What am I missing?
So, this is what you have to do:
DB::table('users')->where(function($query) use ($searchQuery){
$query->where('lname', 'LIKE', $searchQuery . '%')
->orWhere('fname', 'LIKE', $searchQuery . '%')
->orWhere('phone','LIKE', '%' . $searchquery);
})
->get();
Note that I've put use ($searchQuery) so it can be used inside the closure
When you use orWhere with multiple where clauses, you need to be careful!
Where clauses after orWhere doesn't effected to sibling where clauses before orWhere clauses such as where, whereHas, whereDoesntHave, orWhereHas
also you have to pass $searchquery variable inside to function in where clause using use ($searchquery)
Contacts::->where(function ($query) use ($searchquery) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->where('active', '=', '1')
->get();

Kohana 3 ORM - grouping where conditions with parentheses

I'm trying to run a query through the ORM like this:
SELECT * from table where (fname like 'string%' or lname like 'string%')
AND (fname like 'string2%' or lname like 'string2%');
Here's what i have so far:
$results = ORM::factory('profiles');
foreach ($strings as $string) {
$result->where('fname', 'like', "$string%");
$result->or_where('lname', 'like', "$string%");
}
But this doesn't account for the parentheses. Any ideas?
Found the answer.
It's done with Kohana's where_open() and where_close() methods.
It works fine for me .
ORM code sample
$musicslist = ORM::factory('user_music')
->where_open()
->where('title', 'like', '%' . $search . '%')
->or_where('album', 'like', '%' . $search . '%')
->or_where('artist', 'like', '%' . $search . '%')
->where_close()
->and_where('app_userid','=', $userid)
->find_all();
it will create SQL query
SELECT `user_musics`.* FROM `user_musics` WHERE (`title` LIKE '%as%' OR `album` LIKE '%as%' OR `artist` LIKE '%as%') AND `app_userid` = '21'
Couldn't get code formatting to work in the comment - just thought I'd add a simple example to the answer in case anyone else comes across it:
$query = DB::select()
->from('some_table')
->where_open()
->where('column_one', '=', 1)
->or_where('column_two', '=', 2)
->where_close();
would produce the following SQL:
SELECT * FROM some_table
WHERE (column_one = 1 OR column_two = 2);

Categories