How to search using session with multiple table with where caluse using Laravel? - laravel-5.8

How to do this?
**This code is working **
$posts = NewJob::with(['client_info','office_info'])
->orderBy('won', 'desc')
->where('progress' , 'forwarded')
->where('won', 'like', '%' . $request->session()->get('search') . '%')
->paginate(5);
========================================================================
But when I added more where clause then displaying the blank results.
$posts = NewJob::with(['client_info','office_info'])
->orderBy('won', 'desc')
->where('progress' , 'forwarded')
->where('won', 'like', '%' . $request->session()->get('search') . '%')
->where('proj_name', 'like', '%' . $request->session()->get('search') . '%')
->paginate(5);

This way you can do it.
You can also use one orWhere condition instead of two, according to your need.
$posts = NewJob::with('client_info','office_info')
->orderBy('won', 'desc')
->where('progress' , 'forwarded')
->where('won', 'like', '%' . $request->session()->get('search') . '%')
->orWhere('proj_name', 'like', '%' . $request->session()->get('search') . '%')
->paginate(5);
For joins mean to say relations you can do it something like this.
I have written the code for one of your model, you can do it for other models same like this, and also you can use the closure function in with() function as well. Also you can use orWhereHas on different models as well.
$posts = NewJob::whereHas('client_info', function($query){
$query->where('something','is_something'); //query conditions on other table
$query->orWhere('something','is_something');
})
->orderBy('won', 'desc')
->where('progress' , 'forwarded')
->where('won', 'like', '%' . $request->session()->get('search') . '%')
->orWhere('proj_name', 'like', '%' . $request->session()->get('search') . '%')
->paginate(5);

Related

Laravel eloquent real time search filtering

I'm trying to make a server-side searching and came up with this eloquent query...
public function search(Request $request) {
$data = User::with('instruments')
->where('user_type', 'teacher')
->orWhere('fname', 'like', '%' . $request->term . '%' )
->orWhere('fname', 'like', $request->term . '%' )
->orWhere('lname', 'like', '% ' . $request->term . '%' )
->orWhere('lname', 'like', $request->term . '%' )
->orWhere('email', 'like', '% ' . $request->term . '%' )
->orWhere('email', 'like', $request->term . '%' )
->orWhere('profession', 'like', '%' . $request->term . '%' )
->orWhere('profession', 'like', $request->term . '%' )
->limit(10)
->get();
return response()->json($data);
}
I almost achieved what I wanted. For example, when I search john it gives me all the users with john in their name. The problem is that, tho the search is correct and knowing I declared where('user_type', 'teacher'), the result set still gives me users with student user_type
The problem is that the OrWhere nullifies the user_type condition, that's why it selects users that are not teachers when one of the OrWhere conditions applies.
By using a second where with a closure, it will always only select users with a user_type teacher:
public function search(Request $request)
{
$data = User::with('instruments')
->where('user_type', 'teacher')
->where(function ($query) use ($request) {
$query->where('fname', 'like', '%' . $request->term . '%')
->orWhere('fname', 'like', $request->term . '%')
->orWhere('lname', 'like', '% ' . $request->term . '%')
->orWhere('lname', 'like', $request->term . '%')
->orWhere('email', 'like', '% ' . $request->term . '%')
->orWhere('email', 'like', $request->term . '%')
->orWhere('profession', 'like', '%' . $request->term . '%')
->orWhere('profession', 'like', $request->term . '%');
})
->limit(10)
->get();
return response()->json($data);
}

Laravel Eloquent query ignoring column value

On my search method in the Models\Article::class I'm able to get all the results, even in the multiple relations with Categories and Tags, but the issue, that I've been trying to solve, unsuccessfully, is that I just want the results for the published articles – my db column for this is a bool 'active'.
I've tried different approaches by switching the ->where('active', 1) clause around but the results are the same. It brings all the active and non active.
I have in development 8 articles but only 7 with active = 1. When I perform the search, the response sends all the 8, ignoring the where('active', 1) clause. Probably I'm missing or messing something here in the following method:
// Method in the Article Model class
public function getSearchResults($query)
{
return $this->where('active', 1)->where('start_publishing', '<=', Carbon::now())
->where('external_reference', 'LIKE', '%' . $query . '%')
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%')
->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
})
->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
})
->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
})
->with('images')
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
}
Thanks in advance fr any help here on this issue.
Result is because you are use orWhere Criteria. For fix it use this
$this->where('active', 1)
->where('start_publishing', '<=', Carbon::now())
->where(function($q) use ($query) {
$q->where('external_reference', 'LIKE', '%' . $query . '%') // the $q here is required
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%');
})
->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
})
->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
})
->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
})
->with('images')
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
Ok, after Davit's suggestion, which at the beginning seemed like to work, but it was the query I was doing that was "faking" the results, I finally managed to make it work.
Taking the code David suggested, I made a few changes, through the logic of how the query gets constructed and ended up with a final working method that will return only the active Articles matching the "query" criteria from the request. Below the final method code:
// Method in the Article Model class
public function getSearchResults($query)
{
return $this->where('active', 1)
->where('start_publishing', '<=', Carbon::now())
->where(function ($q) use ($query) {
$q->where('external_reference', 'LIKE', '%' . $query . '%')
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%');
$q->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
});
$q->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
});
$q->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
});
})
->with('categories')
->with('tags')
->with('images')
->distinct()
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
}

Multiple where inside a where statement

I would like to generate a query in Laravel that gives the following results:
From the results obtained from
$results = DB::table('records')->where('email', '!=', 'test#gmail.com')->get();
Do multiple where statements like this:
->where('id', 'like', '%'.$request->search.'%')
->orWhere('email', 'like', '%'.$request->search.'%')
->orWhere('recordType', 'like', '%'.$request->search.'%')
->orWhere('uploadDate', 'like', '%'.$request->search.'%')
->orWhere('uploadTime', 'like', '%'.$request->search.'%')
->get();
How should I phrase the statement in controller to produce the desired result?
Use the where() closure to group parameters:
$results = DB::table('records')->where('email', '!=', 'test#gmail.com')
->where(function($q) use($request) {
$q->where('id', 'like', '%' . $request->search . '%')
->orWhere('email', 'like', '%' . $request->search . '%')
->orWhere('recordType', 'like', '%' . $request->search . '%')
->orWhere('uploadDate', 'like', '%' . $request->search . '%')
->orWhere('uploadTime', 'like', '%' . $request->search . '%');
})
->get();

How to force a where clause on a collection in laravel 5

I have a table that I am running a search on. Foo. I want to check if the search term is like a bunch of different fields in the table. But I want the results to be forced to be "active".
I am using Laravel 5.0
$foos = DB::table('foo')
->where('name', 'LIKE', '%' . $query . '%')
->orWhere('description', 'LIKE', '%' . $query . '%')
->orWhere('address_city', 'LIKE', '%' . $query . '%')
->orWhere('short_description', 'LIKE', '%' . $query . '%')
->orWhere('interview', 'LIKE', '%' . $query . '%')
->get();
$foos = $foos->where('active', true)->get();
Collection has a where method similar to query builder.
$activeFoos = $foos->where('active', true);
https://laravel.com/docs/5.4/collections#method-where
Edit :
I understand what you need from the comments now. Try the code below, and also note that i changed your original $query to $find. So keep that in mind.
$foos = DB::table('foo')
->where('active', true)
->where(function ($query) use ($find) {
$query->where('name', 'LIKE', "%{$find}%")
->orWhere('description', 'LIKE', "%{$find}%")
->orWhere('address_city', 'LIKE', "%{$find}%")
->orWhere('short_description', 'LIKE', "%{$find}%")
->orWhere('interview', 'LIKE', "%{$find}%");
})
->get();
Is the value of active actually true or just truthy (such as an integer of 1)?
For loose comparisons then you'll need to use whereLoose('active', true), otherwise Laravel's method where() on Collections will perform strict type comparisons.
Like Sandeesh said, you'll need to remove the get() since get() is a query builder method, Collections where() methods return a new collection not a builder instance.

Laravel 4: A where and whereIn inside a whereHas using Eloquent

Laravel 4 - advanced Where
I'm trying to retrieve Posts that have a $keyword like a certain Companion and besides that I want to retrieve the Posts the have a linking title or content as the $keyword.
But when I try to use a where or whereIn inside a whereHas the query doesn't take these into account. When the state is 0 (not visible) or not inside the category 1 the Post item should not get selected.
$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');
The code block below has to do two things:
Search for Post items with a title or content like the $keyword
and search for Post items that have Companions like the $keyword
code:
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', 1)
->whereIn('category_id', array(1));
})
->whereIn('category_id', array(1))
->orwhere('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' )
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
The code above retrieves data succesfully except for the where and whereIn parts inside the whereHas.
Who can help me out?
wrap your orWhere clauses in (..)
you don't need where and whereIn in the whereHas closure, since it queries companions table
you don't need whereIn for category_id, unless you want to pass multiple ids there
.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id);
})
->whereIn('category_id', array(1)) // why not where(..) ?
->where(function ($q) use ($keyword) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
Thanks to Jarek Tkaczyk.
His answer was almost correct. All I had to do was wrap the where inside a orWhere. Now I get the Posts that has a Companion like the $keyword and I get the Posts that has the $keyword inside the content or title.
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();

Resources