Complex query in Laravel using Query Builder - php

On my website I am trying to implement search functionality. When someone enters keyword I am supposed to find all posts and users that may match that keyword based on posts title or body, or based on users name, username or bio. However I am having trouble with executing this query.
public function search(Request $request) {
$keyword = $request->get('q');
$posts = Post::where('deleted', 0)
->where('title', 'like', '%'.$keyword.'%')
->orWhere('body', 'like', '%'.$keyword.'%')
->orderBy('title')
->get();
$users = User::where('name', 'like', '%'.$keyword.'%')
->where('active', 1)
->orWhere('username', 'like', '%'.$keyword.'%')
->orWhere('bio', 'like', '%'.$keyword.'%')
->orderBy('username')
->get();
return view('searchresults', compact('users', 'posts'));
}
The specific problem I am having is that after function search $posts variable contains posts which were deleted.
This is what I actually need:
select * from posts where deleted = 0 and (title like $keyword or body like $keyword)
I appreciate any help.
Thanks. :)

Use closure to group where() and orWhere() like this:
$posts = Post::where('deleted', 0)
->where(function($q) use($keyword) {
$q->where('title', 'like', '%' . $keyword . '%')
->orWhere('body', 'like', '%' . $keyword . '%');
})
->orderBy('title')
->get();

public function search(Request $request) {
$keyword = $request->get('q');
$posts = Post::whereNull('deleted')
->where('title', 'like', '%'.$keyword.'%')
->orWhere('body', 'like', '%'.$keyword.'%')
->orderBy('title')
->get();
$users = User::where(function($query) use($keyword){
$query->where('name', 'like', '%'.$keyword.'%');
$query->orWhere('username', 'like', '%'.$keyword.'%');
$query->orWhere('bio', 'like', '%'.$keyword.'%');
})
->where('active', 1)
->orderBy('username')
->get();
return view('searchresults', compact('users', 'posts'));
}
Hope it's help you!!

Related

Laravel Search Query which meets conditions

Hi I have this code that is to search for records.
Admins are able to see all records, this is the query for admins
$trainings = Training::where('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->paginate();
Whereas Employees should only see their records, so how do I add this condition into the existing query for the search function? This is the query I have for the Employee search currently
$employees = Training::where('staffName',$username)
->orWhere('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->paginate();
I have another version for Managers, which is meant to also refer to their roles from a different table. I am trying it like this but it only is working properly when it comes to the Manager, but when the function fetches anything related to staff under them it fetches all records instead of specific records.
$managers = DB::table('trainings')
->join('users','users.id','=','trainings.staff_id')
->select('trainings.staffName','programTitle','trainer','trainingDate','hours')
->where('trainings.staffName',$username)
->orWhere('reportingTo',$username)
->where(function ($query) use ($request) {
$query->where('trainings.staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
Your usage of orWhere is wrong because orWhere remove all conditions if it's true try this code below
Training::where('staffName', $username)
->where(function ($query) {
$query->where('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
For the last query
$managers = DB::table('trainings')
->join('users', 'users.id', '=', 'trainings.staff_id')
->select('trainings.staffName', 'programTitle', 'trainer', 'trainingDate', 'hours')
->where(function ($query) use ($username) {
$query->where('trainings.staffName', $username)
->orWhere('reportingTo', $username);
})
->where(function ($query) use ($request) {
$query->where('trainings.staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
here's when query scopes comes to the rescue.
class Training extends Eloquent {
public function scopeFilterByRole($query, $roleId, $searchInput)
{
if ($roleId === 3) {
return $query->where('staffName', 'LIKE', "%{$searchInput}%")
}
return $query;
}
}
Then in your controller
Training::where('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->FilterByRole(3, $request->search)
->get();
You can write a global scope for that it will automatically apply in all existing queries:
protected static function boot()
{
parent::boot();
static::addGlobalScope(function (Builder $builder) {
if (auth()->check() and auth()->user()->role == 3) {
$builder->where('staffName', 'LIKE', "%{request()->get('search_name')}%")
}
});
}

Executing query with where, whereHas in Laravel

I'm building a small application in Laravel 5.5 where I'm querying something like this:
$models = Contact::where('first_name', 'LIKE', '%'.$request->search_input.'%')
->orWhere('last_name', 'LIKE', '%'. $request->search_input.'%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'LIKE', '%'.$request->search_input.'%');
})
->whereHas('company', function ($query) {
$query->where('type', '=', 'Research');
})
->orderBy('created_at', 'desc')
->take(50)
->get();
The idea behind is, I want to search the list of contact with first_name, last_name and their company name (which is a relational data) with the search_input and filter them with type = Research in the company list, I mean only those contact should appear which is having company type as research.
But currently I'm getting all the list of contacts without filtering it with type. Help me out with this. Thanks.
You need to use where closure:
Contact::where(function($q) use($request) {
$q->where('first_name', 'like', '%' . $request->search_input . '%')
->orWhere('last_name', 'like', '%' . $request->search_input . '%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'like', '%' . $request->search_input . '%');
});
})
->whereHas('company', function ($query) {
$query->where('type', 'Research');
})
->latest()
->take(50)
->get();

How to use where clause on data which is eager loaded in laravel 5?

I want to display only the users who are admin and according to a search term which is entered in the searchbox
I store the keyword first
$keyword = $request->get('search');
and then
$users = User::with('admin')->where('role', 'super_admin')
->where(function ($query) use($keyword){
$query->where('id', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%");
})->paginate($perPage);
This works as im searching terms from main table that is user table
But i also want to filter it using admin table columns as
$users = User::with('admin')->where('role', 'super_admin')
->where(function ($query) use($keyword){
$query->where('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%")
->orWhere($this->admin->status, 'LIKE', "%$keyword%");
})->paginate($perPage);
This throws an error undefined property admin
Due to this line ->orWhere($this->admin->status, 'LIKE', "%$keyword%");
So how can i use admin column in filtering data
sample eager loading
$users = User::with([
'admin' => function($query) use($keyword) {
$query->where('status', 'LIKE', "%$keyword%")
}
])
->where('role', 'super_admin')
->where(function ($query) use($keyword){
$query->where('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%")
})->paginate($perPage);

Undefined variable: keyword error in laravel 5 when using advanced where clause

I am using advanced where clause with parameter grouping. Here's the example
$users = User::where('role', 'admin')
->where(function ($query){
$query->orWhere('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%")
->paginate($perPage);
});
return view('admin.index', compact('users'));
This part is giving me an error : Undefined variable: keyword
Then i searched on the net and found to to add use($keyword, $perPage) like this
$users = User::where('role', 'admin')
->where(function ($query) use($keyword, $perPage){
$query->orWhere('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%")
->paginate($perPage);
});
But then its showing me error in my view file
Call to undefined method Illuminate\Database\Query\Builder::appends()
throw new BadMethodCallException("Call to undefined method {$className}::{$method}()");
Here is the line causing error in index.blade.php
{{ $users->appends(['search' => Request::get('search')])->links("pagination::bootstrap-4") }}
This error was not present before adding use($keyword, $perPage)
You need to add use() to the closure to make it work:
->where(function($query) use($keyword, $perPage) {
But I'm sure you want to put paginate outside the closure to make the query actually work:
User::where('role', 'admin')
->where(function($query) use($keyword) {
$query->orWhere('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%");
})
->paginate($perPage);
You are paginating inside closure. Try paginate at the end like the following-
$users = User::where('role', 'admin')
->where(function ($query) use($keyword){
$query->orWhere('id', 'LIKE', "%$keyword%")
->orWhere('email', 'LIKE', "%$keyword%")
->orWhere('name', 'LIKE', "%$keyword%")
;
})->paginate($perPage);

Fetching first or latest first from many to many relationship in eager loading in laravel

I'm building a small application in laravel 5.4, where I'm having two models Contact and Companies I've many to many relationship between them, something like this in my Contact Model:
public function company()
{
return $this
->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
}
Now at someplace I want to have current company i.e. I want to have latest() first(). Or orderBy, created_by desc and get first() row. For this I have to do something like this:
$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'LIKE', '%'. $request->search_input. '%');
})
->with('company')
->orderBy('created_at', 'desc')
->paginate(50);
foreach ($contacts as $contact)
{
$contact->company = $contact->company()->withPivot('created_at')->orderBy('pivot_created_at', 'desc')->first();
}
For this to remove foreach I tried using a new relation in my Contact model:
public function currentCompany()
{
return $this
->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')
->withTimestamps()
->orderBy('created_at', 'desc')
->first();
}
But while fetching in controller:
$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->orWhereHas('currentCompany', function ($q) use($request) {
$q->where('name', 'LIKE', '%'. $request->search_input. '%');
})
->with('CurrentCompany')
->orderBy('created_at', 'desc')
->paginate(50);
But it is throwing me error, is there any eloquent way or Collection way to remove this foreach.
use first() inside closure-
$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->with(['company'=>function ($q) use($request) {
$q->where('name', 'LIKE', '%'. $request->search_input. '%')->first();
}])
->orderBy('created_at', 'desc')
->paginate(50);
Or like this-
$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'LIKE', '%'. $request->search_input. '%');
})
->with(['company'=>function($q){
$q->first();
}])
->orderBy('created_at', 'desc')
->paginate(50);
this way you don't need to do any additional foreach loop.
You can't really do what you are trying to do directly. The relationships are query shortcuts. You can ensure you only get the first but that will still be a part of a collection if your relationship is one/many to many.
Consider this:
$contacts = Contact::where('name', 'LIKE', '%'. $request->search_input. '%')
->orWhere('email', 'LIKE', '%'. $request->search_input. '%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'LIKE', '%'. $request->search_input. '%');
})
->with([ 'company' => function ($query) {
return $query->latest(); //Take the latest only
})
->orderBy('created_at', 'desc')
->paginate(50);
However to access the company you need to do something like $contacts[index]->company->first() because it will still produce a collection of related companies even.
The problem is the ORM will perform 2 queries, the first one will retrieve all the Contact models which fulfil the query and the 2nd one will retrieve all related models for all contacts it retrieved. This means if you do things like limiting it to 1 related result you might end up only getting one for all contacts retrieved.

Categories