$search = Workforce::whereHas('task_schedule', function($q) use($request){
if($request->search_schedule_day != null){
foreach($request->search_schedule_day as $day){
/*$q->whereIn('schedule_day', 'like', '%'. $day .'%');*/
var_dump($day);
}
}
if($request->search_time_in != null){
$q->where('schedule_time_in', 'like', '%' . $request->search_time_in . '%');
}
if($request->search_time_out != null){
$q->where('schedule_time_out', 'like', '%' . $request->search_time_out . '%');
}
})
I am currently making an advanced search feature, and I have encountered this problem wherein I have an array of search_schedule[] . Then in the database, it is saved one day per row. How do I set a query for searching the days in that array ?
Edit
if($request->search_schedule_day != null){
foreach($request->search_schedule_day as $day){
/*$q->whereIn('schedule_day', 'like', '%'. $day .'%');*/
var_dump($day);
}
}
How can I set a query in the above block of code wherein it checks rows in database where column = $request->search_schedule_day
If you are trying to check whether the model's schedule_day is present in $request->search_schedule_day, you can try this:
if($request->search_schedule_day != null) {
$q->where(function($query)use($request) { // group orWheres
for ($request->search_schedule_date as $day) {
$query->orWhere('schedule_day', $day);
}
}
}
Related
I have here a query that retrieves products with search and another query where fields are not equal to zero. However, I have one field (srp) where it should return data if values is not equal to a zero decimal value. The srp field is a price value by the way, that is why it's decimal. Here's my query.
public function render()
{
$searchTerm = '%' . $this->searchTerm . '%';
$products = Product::where(function($query) use ($searchTerm) {
return $query->where('code', 'like', $searchTerm)
->orWhere('name', 'like', $searchTerm)
->orWhere('description', 'like', $searchTerm);
}) ->where(function($query) {
return $query->where('qty_on_hand', '!=', 0)
->orWhereRaw('srp != FLOOR(0.00)');
})
->orderBy('name')
->paginate($this->amount);
$this->emit('productStore');
return view('livewire.load-in-stock-products', ['products' => $products]);
}
Here's the line of code I'm trying to fix: ->orWhereRaw('srp != FLOOR(0.00)');
FYI, the ->orWhereRaw('srp != FLOOR(0.00)'); is already working. Sorry as I didn't know. It was because I was looking at another field, cost. Again, the query on the question above is already the correct answer.
I'm building a project with Laravel 8. I need to handle request parameter which contains page, limit, searchColumn, searchText, orderColumn and orderDirection. Sometimes I need to search in relations. I've built this system below but it doesn't work on relationships.
$query = (new Log())->newQuery();
$query->with('customer', 'domain', 'type');
if ($request->searchColumn != "" && $request->searchColumn != NULL) {
$query->where($request->searchColumn, 'LIKE', '%' . $request->searchText . '%');
}
if ($request->orderColumn != "" && $request->orderColumn != NULL) {
$query->orderBy($request->orderColumn, $request->orderDirection);;
}
$logs = $query->paginate($request->limit, ['*'], '', $request->page);
For example if $request->searchColumn is 'customer.name', how should I search? I've tried few ways but they don't work.
You may use whereHas method which allow to define additional query constraints on your relation. E.g:
$searchText = $request->searchText;
if ( explode( '.', $request->searchColumn )[0] == 'customer' ) {
$query->whereHas( 'customer', function( $query ) use ( $searchText ) {
$query->where( 'name', 'LIKE', '%' . $searchText . '%' )
});
}
Im working on product filtering using AJAX. Is there any possible way to produce same output as picture shown below using query builder?
I have tried union but it’s not working.
I hope this example gives idea , Try this one
use multiple if statement and get data into DB using joins .
function datatables($request) {
$data = $this->leftJoin('blog_category', 'blog_category.blog_category_uuid', '=', 'blog_detail.blog_category_uuid')
->where('blog_detail.blog_detail_is_deleted', 'NO');
if ($request->search['value'] != null && $request->search['value'] != '') {
$keyword = $request->search['value'];
$data = $data->where(function($query) use ($keyword) {
// $query->orWhere('activity_type.activity_type_name', 'LIKE', '%' . $keyword . '%');
$query->orWhere('blog_detail.blog_detail_title', 'LIKE', '%' . $keyword . '%');
});
}
if (isset($request->order[0]['dir'])) {
$data = $data->orderBy('blog_detail.blog_detail_id', $request->order[0]['dir']);
} else {
$data = $data->orderBy('blog_detail.blog_detail_created_date');
}
$datacount = $data->count();
$dataArray = $data->select('blog_detail.*', 'blog_category.blog_category_name' , DB::raw('DATE_FORMAT(blog_detail.blog_detail_created_date,"%Y-%m-%d") as blog_detail_date'));
if ($request->length == -1) {
$dataArray = $dataArray->get();
} else {
$dataArray = $dataArray->skip($request->start)->take($request->length)->get();
}
return [$datacount, $dataArray];
}
In laravel you can create a model for product say Product. Then the query will be like
$products = Product::where('product_status', '1');
if ($request->input('minimum_price') && $request->input('maximum_prize')) {
$products = $products->whereBetween('product_prize', array($request->input('minimum_price'), $request->input('maximum_prize')));
}
if ($request->input('brand')){
$brand_filter = implode("','", $request->input('brand'));
$products = $products->whereIn('product_brand', $brand_filter);
}
$products = $products->get();
after the execution $products contains the products after query.
I have a GET form with three filters.
make
Year
country
I need to get all posts from db. But filter the results based on these three filters.
If a country is selected, get posts for that country only or all countries.
if a make is selected, get posts for that make only or all makes
if a year is selected, get posts for that year only or all years
how to write one query that filters all these three options. What I have done is used if and else statements and written different queries for each scenario. That's 9 queries to get one information. Can we make it dynamic and just have one query?
My Example query:
public function search(Request $request)
{
$search=$request->input('search');
if($request->input('country') == "all")
{
$posts = Post::where('status','Published')->orderBy('status_change','DESC')
->where('status','Published')
->where(function($query) use ($search){
$query->where('title','LIKE','%'.$search.'%');
$query->orWhere('model','LIKE','%'.$search.'%');
$query->orWhere('notes','LIKE','%'.$search.'%');
$query->orWhere('description','LIKE','%'.$search.'%');
})
->paginate(25);
}
else
{
$posts = Country::where('country_name', $request->input('country'))->first()->posts()->orderBy('status_change','DESC')
->where('status','Published')
->where(function($query) use ($search){
$query->where('title','LIKE','%'.$search.'%');
$query->orWhere('model','LIKE','%'.$search.'%');
$query->orWhere('notes','LIKE','%'.$search.'%');
$query->orWhere('description','LIKE','%'.$search.'%');
})
->paginate(25);
}
return view('welcome')
->with('published_posts',$posts)
;
}
I think something like this would work:
/**
* #param Request $request
*/
function search(Request $request)
{
$postsQuery = Post::where('status', 'Published');
if ($request->has('country')) {
$country = $request->country;
// assuming relationships are setup correclty
$postsQuery->whereHas('country', function ($query) use ($country) {
$query->where('country_name', 'LIKE', $country);
});
}
if ($request->has('search')) {
$postsQuery->where(function ($query) use ($search) {
$query->where('title', 'LIKE', '%' . $request->search . '%');
$query->orWhere('model', 'LIKE', '%' . $request->search . '%');
$query->orWhere('notes', 'LIKE', '%' . $request->search . '%');
$query->orWhere('description', 'LIKE', '%' . $request->search . '%');
});
}
$postsQuery->orderBy('status_change', 'DESC')->paginate(25);
return view('welcome')->with('published_posts', $result);
}
I used 'when' method.
$make = null;
$year = null;
$country = null;
if($request->filled('make')){
$make = $request->query('make');
}
if($request->filled('year')){
$year = $request->query('year');
}
if($request->filled('country')){
$country = $request->query('country');
}
$posts = DB::table('posts')
->when($make, function($query, $make){
return $query->where("make", "=", $make);
})
->when($year, function($query, $year){
return $query->whereYear("year", "=", $year);
})
->when($country, function($query, $country){
return $query->where('country', "like", $country);
})
->get();
Check out the Laravel Docs:
Check out an article here
I want to add a condition first before adding another where query it but seems that laravel won't allow. I want something like the following.
function index()
{
$role = Auth::user()->role; //SEE_ALL, SEE_DEPT_A, SEE_DEPT_B
$q = Input::get('q');
if ($q != "") {
$user = User::where('name', 'LIKE', '%' . $q . '%')
->orWhere('email', 'LIKE', '%' . $q . '%')
->orderBy('name');
if ($role == "SEE_DEPT_A") {
$user->where('user_department', "A");
}
$user->paginate(10)->appends('q', $q);
}
return view('users.index')->with('data', ['users' => $user, 'q' => $q]);
}
You should use the when() function :
$user = User::where('name', 'LIKE', '%' . $q . '%')
->orWhere('email', 'LIKE', '%' . $q . '%')
->orderBy('name')
->when($role == "SEE_DEPT_A", function($query){
return $query->where('user_department', "A");
})->paginate(10)->appends('q', $q);
You need to assign the statement where you used where clause:
if ($role == "SEE_DEPT_A") {
$user = $user->where('user_department', "A");
}
And if the if statement does not run (in case of false), it will throw an error about $user being undefined
You can insert multi conditions like this:
$user_model = User::where('name', 'LIKE', '%' . $q . '%')
->orWhere('email', 'LIKE', '%' . $q . '%')
->orderBy('name');
if ($role == "SEE_DEPT_A") {
$user_model = $user_model->where('user_department', "A");
}
...
//other if statements
...
$users = $user_model->paginate(10)->appends('q', $q);