Laravel: How to search DB for a given string using eloquent? - php

I would like to retrieve data matching the search string, provided, it must not belongs to current user.
if($str_search && $str_search != '' )
{
$mandate_list = Mandate::select(array('umrn','mandate_status', 'payment_type','currency','fixed_amount','max_amount','user_id','id','debtor_bank','creditor_bank','refusal_reason'))
->orWhere('id', 'LIKE', '%'.$str_search.'%')
->orWhere('umrn','LIKE','%'.$str_search.'%')
->orWhere('mandate_status','LIKE','%'.$str_search.'%')
->orWhere('mandate_date','LIKE','%'.$str_search.'%')
->orWhere('payment_type','LIKE','%'.$str_search.'%')
->orWhere('currency','LIKE','%'.$str_search.'%')
->orWhere('fixed_amount','LIKE','%'.$str_search.'%')
->orWhere('mandate_date','LIKE','%'.$str_search.'%')
->where('user_id', '<>', $current_user)
->orderBy('updated_at', 'desc')
->get();
}
But this query displays all records (even belongs to current user).
How can i solve this. Thanks for your suggestions.

Group all your orWheres in a closure:
Mandate::select([.....])->where(function($query) use ($str_search)
{
$columns = ['id', 'umrn', 'mandate_status', ...];
foreach ($columns as $column)
{
$query->orWhere($column, 'LIKE', '%'.$str_search.'%');
}
})
->where('user_id', '<>', $current_user)
->orderBy('updated_at', 'desc')
->get();

Related

Laravel Eloquent hardcoded value in whereNotIn

In Laravel eloquent query using multiple columns for whereNotIn clause I need to hardcoded one of the DB::raw column for the value should be coming from a variable (loop variable). What is the best way to implement this?
This is my query and I need to change the hardcoded 1 in DB::raw('(1,user_profile.user_id')
$otherProfiles = Userprofile::where('user_id', '!=', $profile->user_id)
->where(function ($query) use ($userInterests) {
foreach ($userInterests as $interest) {
$query->orWhere('interest', 'like', "%$interest%");
};
})
->whereNotIn(DB::raw('(1, user_profile.user_id)'), function ($query) {
$query->select('sender_id', 'receiver_id')
->from('email_reports');
})
->inRandomOrder()
->get();
Manage to fix it by just simple concatenation
the code added were DB::raw('('. $profile->user_id . ', user_profile.user_id)')
create a variable as $id for your dynamic value..
$otherProfiles = Userprofile::where('user_id', '!=', $profile->user_id)
->where(function ($query) use ($userInterests) {
foreach ($userInterests as $interest) {
$query->orWhere('interest', 'like', "%$interest%");
};
})
->whereNotIn('user_id', $id), function ($query) {
$query->select('sender_id', 'receiver_id')
->from('email_reports');
})
->inRandomOrder()
->get();

Laravel where clause is not returning data after multiple orWhere clauses?

I am using laravel 6.10 version in that I am implementing search,
I have 3 tables
1) course_category
2) course_sub_category
3) course [course_id_category(foreign key),course_sub_category_id(foreign key)]
below is my code
$course = $request->searchItem;
if ($course=="") {
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->orderBy('course.title','asc')
->paginate(15);
}
else{
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where('course.title', 'LIKE', '%'.$course.'%')
->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
->get();
}
when i am retunring the values i am gatting 0 arrays but when i am removing one orwhere from existing my query is working and its returnig we all values with match
means when i am using multiple orWhere in laravel my where is not working, please share the solution on same.
->orWhere doesnt work like typical SQL. You should use it like that:
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where(function($query) use ($course){
$query->where('course.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
})
->get();

Call to a member function where() on array laravel

I have two tables tbl_law_master & tbl_law_sub_master
on both these tables there is a column named as assigned_to, this column stores user's id.
my task is to get sublaw from tbl_law_sub_master for particular user's id by joining law_id of tbl_law_master.
this is my code,
$law_id = $_GET['law_id'];
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id);
}
it shows me error as
Call to a member function where() on array
The problem is that you already called get() of your query then called where() again.
You should use where clause function like this
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where(function($query) use ($law_id) {
$query->where('tbl_law_sub_master.lm_id', $law_id);
if (!in_array(Auth::user()->role, [1, 7]))
{
$query->where('tbl_law_master.assigned_to', Auth::user()->id);
}
})
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
I think you should call get after the last where statement
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc');
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id)->get();
}

Laravel join with "and" and "or" in the where clause

How can I write a Laravel 5 Eloquent query like this SQL?
where ( (table1.fname like %xxxxx% )
OR (table1.lname like %xxxxx%) )
AND table1.is_active != 0
I have already tried
->where('users.is_active', '!=', 2)
->where('users.name', 'like' , '%'. $search .'%')
->orWhere('users.lname', 'like' , '%'. $search .'%')
->orWhere('users.email', 'like' , '%'. $search .'%') ->get();
Take a look at the Advanced join Clauses in Laravel Documentation
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
joins
EDIT
I think i misunderstood the question. To do nested wheres you can do the following thing:
\DB::table('table1')->...->where(function($query) {
$query->where('table1.fname', 'like', '%xxxxx$')->orWhere('table1.lname', 'like', '%xxxx%);
})->where('table1.is_active', '!=', 0);
You can use Model for this:
TableModel::where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
$data = DB::table('table1')->where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
//add use DB; in your controller
To have grouped sub-clauses that should evaluate as a whole, you can pass where() a closure. Then you want a second where call for the check on the is_active column:
$value = '%xxxxx%';
$collection = Model::where(function ($models) use ($value) {
$models->where('fname', 'like', $value)
->orWhere('lname', 'like', $value);
})
->where('is_active', '!=', 0)
->get();

whereHas query in Laravel

Hello guys,
$filterArray = explode("_", $filters);
$data['articles'] = \DB::table('products')->join('product_category', function ($q) {
$q->on('product_category.product_id', '=', 'products.id');
})->where('product_category.category_id', '=', $id)
->select('products.*')
->whereBetween('price_retail_1', array($priceFrom, $priceTo))
->whereHas('filters', function ($query, $filterArray) {
$query->whereIn('filter_id', $filterArray);
})
->orderBy('products.' . $sort, $sortOrder)
->get();
}
I have the following query and I'm having some issues on the whereHas method. I'm getting an error
Unknown column 'has' in 'where clause
most likely because the $filterArray variable is out of scope for the function ( or at least that is what I am guessing. Any help on how to solve the issue is appreciated.
You cannot use whereHas method in the Query Builder context. The whereHas method is only for Eloquent Query Builder that is comming from the Eloquent models and their relationships.
What you can do is to use joins. So you can try like this:
$filterArray = explode("_", $filters);
$data['articles'] = \DB::table('products')->join('product_category', function ($q) {
$q->on('product_category.product_id', '=', 'products.id');
})->where('product_category.category_id', '=', $id)
->select('products.*')
->whereBetween('price_retail_1', array($priceFrom, $priceTo))
->join('filters', 'products.filter_id', '=', 'filters.filter_id')
->whereIn('filter_id', $filterArray);
->orderBy('products.' . $sort, $sortOrder)
->get();
I don't know how you connecting these two tables so here is only the example data:
->join('filters', 'products.filter_id', '=', 'filters.filter_id')

Categories