Select Join Where and orWhere in Laravel 5 - laravel

I have two tables- jobseekers and resumes. I try to achieve three search options- by entering 1. "first name", 2. "last name", and 3. "first name + last name". My current code is as below:
$q = \Request::get('keyword');
$data['resume'] = Resume::join('jobseekers', 'jobseekers.user_id', '=', 'resumes.user_id')
->where('jobseekers.first_name','like','%'.$q.'%')
->orWhere('jobseekers.last_name','like','%'.$q.'%')
->orderBy('resumes.updated_at','desc')->paginate(50);
Using my texbox (keywords), When I search only last / first name, it works fine. However, when I type both first + last name in the textbox, it shows no result.
Please share me how to achieve this.

Using CONCAT():
->orWhere(DB::raw("CONCAT(jobseekers.first_name, ' ', jobseekers.last_name)"), 'LIKE','%'.$q.'%');

Use where and or where like this
$results = App\Table::select('*')
->where(function ($query) use ($search_term) {
$query->where('column3', 'like', $search_term.'%')
->orWhere('column4', 'like', $search_term.'%')
})
->orderBy('column1', 'asc')
->get();
Hope this help

Related

Wildcard-like syntax in an eloquent Where clause to search between two strings

I saw the answer provided in this question Wildcard-like syntax in an eloquent Where clause? now I want to know if is there a way to search between two strings?.
basicasicaly in my code I want to show requests that have a status of new or scheduled.
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where('status', 'LIKE','%New%')
->get();
you can use whereIn ,The whereIn method verifies that a given column's value is contained within the given array:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->whereIn('status', ['new','scheduled'])
->get();
You can use:
->where('status', 'new')
->orWhere('status', 'scheduled')
you can simply use a where with an orWhere:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where(function($q) {
$q->where('status', 'LIKE','%New%');
$q->orWhere('status', 'LIKE','%Scheduled%');
})->get();

A way to determine if "where like" matches 100% with a post in Laravel?

Is there a way to determine if this query:
$news_result = Post::with('image')
->has('image')
->with('tags')
->where('title', 'LIKE', '%' . $search . '%')
->get();
Has a 100% match with some title? If a user searches for "Some random news title" and in the database there is a post with such title to return some kind of marker?
The LIKE operator is used to partially match something.
You should not use it, and do ->where('title', $search) instead.
Remove '%' in your code
$news_result = Post::with('image')->has('image')->with('tags')->where('title', $search)->get();

Laravel Eloquent Where Query with multiple words

I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.

Querying related table data with Eloquent

i have a problem trying to get records from a model based on a related table.
I have two tables one called leads and one called recycle_logs, my app will basically send the same record in the leads table like once a month, and when it does so i'll store a new record in recycle_logs.
The problem is that i need to select all leads with a specific campaign_id value and that have a specific status that is different from invalid, so far so good, now the problem is i need to get them only if they don't have any recycleLogs associated with them OR if the last recycleLog associated with that particular lead is older than 30 days ago for instance.
What i currently have looks somewhat like this.
$leads = $this->leadModel->where(Lead::CAMPAIGN_ID, $this->campaignID)
->where(Lead::DUPLICATED, Lead::DUPLICATED_NO)
->where(Lead::LEAD_STATUS, "!=" ,Lead::LEAD_STATUS_INVALID)
->orderBy(Lead::CREATED_AT, 'desc')
->with(
['leadRecyclingLog' => function($query) {
$query->where(LeadRecyclingLog::CREATED_AT, '<', (new Carbon())->subDays($this->configRecyclingDays))
->orWhere(LeadRecyclingLog::ID, null);
}]
)
->get();
What exactly am i doing wrong? It always selects the same number of records regardless of me adding or removing recycleLogs
I've managed to get it done through a raw SQL query which i'll post below in case it helps anyone, i'd still like to know how to do it in Eloquent/Query Builder.
SELECT * FROM `leads` LEFT JOIN `lead_recycling_logs` ON `leads`.`guid` = `lead_recycling_logs`.`original_lead_guid` WHERE `leads`.`campaign_id` = :campaignID AND `leads`.`duplicated` = 0 AND `leads`.`lead_status` != :invalidStatus AND (`lead_recycling_logs`.`id` IS NULL OR `lead_recycling_logs`.`created_at` < :recyclingDate) ORDER BY `leads`.`created_at` DESC
Try this:
$leads = $this->leadModel->where(Lead::CAMPAIGN_ID, $this->campaignID)
->where(Lead::DUPLICATED, Lead::DUPLICATED_NO)
->where(Lead::LEAD_STATUS, "!=" ,Lead::LEAD_STATUS_INVALID)
->orderBy(Lead::CREATED_AT, 'desc')
->where(function($q) {
$q->whereHas('leadRecyclingLog', function($q) {
$q->where(LeadRecyclingLog::CREATED_AT, '<', (new Carbon())->subDays($this->configRecyclingDays));
})
->orWhereHas('leadRecyclingLog', '<', 1); // Where count of the relationship is smaller than 1
})->get();
I assumed the first part of the query is working well (up until the relationship).
What you're looking for is ->whereHas(relationship), not ->with(relationship). ->with(relationship) will attach the associated results to the original model (the query for the original model will not be affected by ->with()). ->whereHas(relationship) filters the original model by the condition.
Got it to work through #devk 's help
$leads = $this->leadModel->where(Lead::CAMPAIGN_ID, $this->campaignID)
->where(Lead::DUPLICATED, Lead::DUPLICATED_NO)
->where(Lead::LEAD_STATUS, "!=" ,Lead::LEAD_STATUS_INVALID)
->orderBy(Lead::CREATED_AT, 'desc')
->where(function($q) {
$q->whereHas('leadRecyclingLog', function($q) {
$q->where(LeadRecyclingLog::CREATED_AT, '<', (new Carbon())->subDays($this->configRecyclingDays));
})
->doesntHave('leadRecyclingLog', 'or');
})->get();

HOW TO: Laravel 5 - mySQL query

the situation:
I have one mySQL database and I want to get a dinstinct count of the column C_ID where the set timestamp is like the current day (e.g. 2015-08-14)
in phpMyAdmin it works fine:
SELECT COUNT(DISTINCT C_ID) FROM table WHERE touched_at LIKE '2015-08-14%'
the result is 32; but in Laravel 5 I get 320 because the distinct function does not work for me
$date = date('Y-m-d');
$one = DB::table('talbe')
->select()
->where('touched_at', 'like', "$date%")
->distinct('C_ID')
->count();
Can you please assist me with this problem?
Thanks in advance
Timo
This may work. Tried it in one of my projects and that produces the right answer for me.
$one = DB::table('talbe')
->where('touched_at', 'like', "$date%")
->count(DB::raw('distinct C_ID'));
Try this if it works:
$date = date('Y-m-d');
$one = DB::table('talbe')
->select('touched_at')
->where('touched_at', 'like', "$date%")
->distinct('C_ID')
->count();
You may also try this code
$date = date('Y-m-d');
$one = DB::table('table')
->select(DB::raw('count(Distinct C_ID) as C_ID'))
->where('touched_at', 'like', "$date%")
->get();

Resources