Selecting to multiple tables in Laravel - mysql

Trying to select entries from user input $query. I would like to search the first_name, last_name, address (etc) columns that has NULL on deleted_at. What I have done so far is:
$data = DB::table('users')
->where('first_name', 'LIKE', '%' . $query . '%')
->orWhere('last_name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query. '%')
->whereNull('deleted_at')
->get();
My goal is to search the above mentioned columns by the user's query and show any match it finds. Problem is, it also shows entries that are NOT NULL in the column deleted_at. I wish to only see results which are NULL at deleted_at. What may I have been doing wrong?

make use of parameter grouping
$data = DB::table('users')
->whereNull('deleted_at')
->where(function ($query) {
$query->where('first_name', 'LIKE', '%' . $query . '%')
->orWhere('last_name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query. '%');
})
->get();

Related

Laravel 8 whereJsonContains

with this code I implemented a search:
if ($request->keyword != "") {
$users = User::where('firstname', 'like', '%' . $request->keyword . '%')
->orWhere('lastname', 'like', '%' . $request->keyword . '%')
->orWhere('email', 'like', '%' . $request->keyword . '%')
->orderBy('lastname')
->whereJsonContains("out", Auth::user()->id)
->paginate($request->total);
} else {
$users = User::whereJsonContains("out", Auth::user()->id)->orderBy('lastname')->paginate($request->total);
}
Filtering for firstname, lastname and e-mail works fine, but the important where with "->whereJsonContains("out", Auth::user()->id)" not work. After typing "s" every User with "s" will be displayed in results.... what is wrong?
You ay try
if ($request->keyword != "") {
$users = User::query()
->where(function($query) use($request){
$query->where('firstname', 'like', '%' . $request->keyword . '%')
->orWhere('lastname', 'like', '%' . $request->keyword . '%')
->orWhere('email', 'like', '%' . $request->keyword . '%');
})
->whereJsonContains("out", Auth::user()->id)
->orderBy('lastname')
->paginate($request->total);
} else {
$users = User::whereJsonContains("out", Auth::user()->id)
->orderBy('lastname')
->paginate($request->total);
}
Query will return records where either the firstname, lastname or email contains anything like $request->keyword and the out column contains the currently logged in user's id.

Laravel eloquent: Having and where in the same query but I want ti to be OR [duplicate]

Currently I am working on a project in Laravel but I am stuck.I want to create a SQL statement like this:
SELECT * FROM SPITems WHERE publisher_id=? AND feed_id=? AND (title LIKE '%?%' OR description LIKE '%?%')
Now I have this code:
$query = SPItem::orderBy('title');
if(isset($_GET['publisherID']) && is_numeric($_GET['publisherID']))
{
$query = $query->where('publisher_id', $_GET['publisherID']);
}
if(isset($_GET['productFeedID']) && is_numeric($_GET['productFeedID']))
{
$query = $query->where('program_id', $_GET['feedID']);
}
if(isset($_GET['search']))
{
$query = $query->orWhere('title', 'like', '%' . $_GET['search'] . '%');
$query = $query->where('description', 'like', '%' . $_GET['search'] . '%');
}
But that generates:
SELECT * FROM SPITems WHERE (publisher_id=? AND feed_id=?) OR (title LIKE '%?%') AND description LIKE '%?%'
How can I get the correct "or" order?
Check out the Logical Grouping section in the docs:
https://laravel.com/docs/master/queries#logical-grouping
It explains how to group conditions in the WHERE clause.
It should be something like:
if(isset($_GET['search']))
{
$query->where(function($query){
$query->where('title', 'like', '%' . $_GET['search'] . '%')
->orWhere('description', 'like', '%' . $_GET['search'] . '%');
});
}
You can use whereRaw
SPItem::whereRaw(" publisher_id=? AND feed_id=? AND (title LIKE '%?%' OR description LIKE '%?%')", array(?,?,?,?))

laravel sql problem with query where and or, lost condition

im trying to write sql query in laravel:
select * from products where deleted=0 AND
(('name_ru','LIKE', '%' . $request->q_search . '%')
OR ('name_en','LIKE', '%' . $request->q_search . '%')
OR ('name_lv','LIKE', '%' . $request->q_search . '%'))
now i have:
$products = DB::table('product')
->select('id','name_lv', 'name_ru', 'name_en','price','desc')
->where('deleted',0)
->where('name_lv','LIKE', '%' . $request->q_search . '%')
->orWhere('name_ru','LIKE', '%' . $request->q_search . '%')
->orwhere('name_en','LIKE', '%' . $request->q_search . '%')
->distinct()
->orderBy('price', 'desc')
->paginate(10,['*'], 'found')
->onEachSide(6);
my problem is with condition deleted=0, my sql ignores it. Whats wrong? thanks for advice!
you can use where(function(){}) as you have where AND (.. OR ..)
$products = DB::table('product')
->select('id', 'name_lv', 'name_ru', 'name_en', 'price', 'desc')
->where(function ($q) use ($request) {
$q->where('name_lv', 'LIKE', '%' . $request->q_search . '%')
->orWhere('name_ru', 'LIKE', '%' . $request->q_search . '%')
->orwhere('name_en', 'LIKE', '%' . $request->q_search . '%');
})
->where('deleted', 0)
->distinct()
->orderBy('price', 'desc')
->paginate(10, ['*'], 'found')->onEachSide(6);

Query for returning individual records from related table in a polymorphic relationship

I'm currently working with an Activities table which has Thread and Replies records associated with it. When I try to join the tables, I get a mix and match and both records and it doesn't return individual records, but with records containing both Thread and Reply "body" field. I'm hoping someone can see what I'm trying to do with the code I'm providing:
if (request('name')){
$name = request('name');
$user = User::where('name', $name)->firstOrFail();
$activities = \DB::table('activities')
->leftJoin('threads', function($builder) use ($user){
$builder->on('threads.user_id', '=', 'activities.user_id')
->where('activities.user_id', '=', $user->id)
->where('activities.activity_type', '=', 'App\Thread');
})
->leftJoin('replies', function($builder) use ($user){
$builder->on('replies.user_id', '=', 'activities.user_id')
->where('activities.user_id', '=', $user->id)
->where('activities.activity_type', '=', 'App\Reply');
})
->where('replies.body', 'LIKE', '%' . $search . '%')
->orWhere('threads.title', 'LIKE', '%' . $search . '%')
->orWhere('threads.body', 'LIKE', '%' . $search . '%')
->get()->unique('id');
}
I want to return Threads and Replies separately with all the other fields, as sometimes in my experimentation it has led to the user being null or the body being null and I just don't understand why.
Sometimes it will return a App\Thread in the Activities table that I have created as an App\Reply. I'll admit my knowledge of joins is limited so I'm asking if anyone can please help!
I've got this new query here:
$activities = Activity::with('activity')->whereHas('thread', function ($query) use ($search, $user) {
$query->where('threads.user_id', '=', $user->id)
->where('threads.body', 'LIKE', '%' . $search . '%')
->orWhere('threads.title', 'LIKE', '%' . $search . '%');
})->orWhereHas('reply', function ($query) use ($search, $user) {
$query->where('replies.body', 'LIKE', '%' . $search . '%')
->where('replies.user_id', '=', $user->id);
})->get();
Thank you!
After trying to include the user in the query builder, I just added the user after my ::with clause and got the following query to work:
$activities = Activity::with('activity')->where('user_id', $user->id)->whereHas('thread', function ($query) use ($search, $user) {
$query->where('threads.body', 'LIKE', '%' . $search . '%')
->orWhere('threads.title', 'LIKE', '%' . $search . '%');
})->orWhereHas('reply', function ($query) use ($search, $user) {
$query->where('replies.body', 'LIKE', '%' . $search . '%')
->where('replies.user_id', '=', $user->id);
})->get();
Anyone in the future who may need some additional assistance can visit this link: https://zaengle.com/blog/using-wherehas-in-laravel-polymorphic-relations

Need to search multiple words in multiple table and display only those rows which has all the words in laravel

I have an equipment which has type, manufacturer , model etc and it has attributes which are in other tables. I have created a join condition to connect with attributes. Now, if i search for a keyword related to type, manufacturer or model, then it shows correct result. But , i also need to search by attribute. I don't know how to extend this search so that it can also fetch result on the basis of attributes which are in other tables. Another thing is if i right both manufacturer and attribute in search box in one time , then i need result of equipments which has both manufacturer and attribute. Please advise. Any help would be appreciated
Currently my code looks like this:
public function filterEquipementRecords(Request $request)
{
$equipements = Equipment::with('equip_status','equip_type','manufacturer', 'equip_model', 'equip_images', 'customer')
//These are join with different attribute tables
->selectRaw('equipment.*, Fuel.EQATTRPVAL_Value AS EQUIP_Fuel, Cab.EQATTRPVAL_Value AS EQUIP_Cab, Drive.EQATTRPVAL_Value AS EQUIP_Engine, PowerManu.EQATTRPVAL_Value AS EQUIP_Power, CabAttrib.EQATTR_YesNo AS EQUIP_CabYesNo')
// Fuel Type
->leftJoin('equipattributetypes as FuelType', function($join) {
$join->on('equipment.EQTYPE_ID', '=', 'FuelType.EQTYPE_ID')
->whereRaw("FuelType.EQATTRTYPE_Name = 'Fuel Type'");
})
->leftJoin('equipattributes as FuelAttrib', function($join) {
$join->on('equipment.EQUIP_ID', '=', 'FuelAttrib.EQUIP_ID')
->on('FuelType.EQATTRTYPE_ID', '=', 'FuelAttrib.EQATTRTYPE_ID');
})
->leftJoin('equipattributepickvalues as Fuel', function($join) {
$join->on('FuelAttrib.EQATTRPVAL_ID', '=', 'Fuel.EQATTRPVAL_ID');
})
// Cab
->leftJoin('equipattributetypes as CabType', function($join) {
$join->on('equipment.EQTYPE_ID', '=', 'CabType.EQTYPE_ID')
->whereRaw("CabType.EQATTRTYPE_Name = 'Cab'");
})
->leftJoin('equipattributes as CabAttrib', function($join) {
$join->on('equipment.EQUIP_ID', '=', 'CabAttrib.EQUIP_ID')
->on('CabType.EQATTRTYPE_ID', '=', 'CabAttrib.EQATTRTYPE_ID');
})
->leftJoin('equipattributepickvalues as Cab', function($join) {
$join->on('CabAttrib.EQATTRPVAL_ID', '=', 'Cab.EQATTRPVAL_ID');
})
// Drive
->leftJoin('equipattributetypes as DriveType', function($join) {
$join->on('equipment.EQTYPE_ID', '=', 'DriveType.EQTYPE_ID')
->whereRaw("DriveType.EQATTRTYPE_Name = 'Drive'");
})
->leftJoin('equipattributes as DriveAttrib', function($join) {
$join->on('equipment.EQUIP_ID', '=', 'DriveAttrib.EQUIP_ID')
->on('DriveType.EQATTRTYPE_ID', '=', 'DriveAttrib.EQATTRTYPE_ID');
})
->leftJoin('equipattributepickvalues as Drive', function($join) {
$join->on('DriveAttrib.EQATTRPVAL_ID', '=', 'Drive.EQATTRPVAL_ID');
})
//Power Manu
->leftJoin('equipattributetypes as PowerManuType', function($join) {
$join->on('equipment.EQTYPE_ID', '=', 'PowerManuType.EQTYPE_ID')
->whereRaw("PowerManuType.EQATTRTYPE_Name = 'Power Manu'");
})
->leftJoin('equipattributes as PowerManuAttrib', function($join) {
$join->on('equipment.EQUIP_ID', '=', 'PowerManuAttrib.EQUIP_ID')
->on('PowerManuType.EQATTRTYPE_ID', '=', 'PowerManuAttrib.EQATTRTYPE_ID');
})
->leftJoin('equipattributepickvalues as PowerManu', function($join) {
$join->on('PowerManuAttrib.EQATTRPVAL_ID', '=', 'PowerManu.EQATTRPVAL_ID');
})
// This is my query where i am searching on the basis of keyword, here i want to add attribute (which are there with joins) search also
->where(function ($query) use ($request) {
if($this->quick_search_enable){
if ($request->quick_search != "") {
$query->where('equipment.EQUIP_ID', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_AddedOn', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Year', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_VendorYear', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Cost', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Price', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_EmailPrice', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Size', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Hours', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_SerialNumber', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_VendorEquipNumber', 'like', '%' . $request->quick_search . '%')
->orWhere('EQUIP_Length', 'like', '%' . $request->quick_search . '%')
// These are relations in equipment model
$query->orWhereHas('manufacturer', function ($query) use ($term) {
$query->where('MANU_ShortName', 'like', '%' . $term . '%');
});
$query->orWhereHas('equip_type', function ($query) use ($term) {
$query->where('EQTYPE_Desc', 'like', '%' . $term . '%');
});
$query->orWhereHas('equip_model', function ($query) use ($term) {
$query->where('EQMODEL_ModelNumber', 'like', '%' . $term . '%');
});
$query->orWhereHas('equip_status', function ($query) use ($request) {
$query->where('EQSTATUS_Name', 'like', '%' . $request->quick_search . '%');
});
}
}
});