how to convert this query in eloquent? - php

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

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.

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

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

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

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