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();
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.
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();
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.
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);