I have 3 tables that are connected/have relation.
Posts table have many tag and one Category
Category table have many Posts
Tag table have many Posts
i want a search feature, i know how to search only use Posts (based on title).
I tried to search each tables with Where in my controller but still no luck.
public function Search(Request $request)
{
$search = $request->search;
$posts = post::where('title', 'like', "%{$search}%")->paginate(5);
return view('search', compact('posts'))->with('result', $search);
}
For example
i have a post Titled 'Test' and with Category 'Tost' and with Tags 'Tast and Tust'
so if i type either the title, category or tags i want it to show up. how can i achieve it?
Try use whereHas method (for further info check https://laravel.com/docs/5.8/eloquent-relationships#querying-relations)
So would become something like:
Post::query()
->where('title', 'like', "%$search%")
->orWhereHas('categories', function ($query) use ($search) {
$query->where('name', 'like', "%$search%");
})
You must use like this.
Post::where('title', 'like', '%' . Input::get('search') . '%')->get();
Related
I am currently writing a search query but I'm having issues joining a where with a whereHas
if I wanna search for categories only..
The below query works if I just search for categories solo
$goals = $myGoals
->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->paginate(10);
if I wanna search for the title only..
Same as above if I wanna search for the title of goals solo
$goals = $myGoals->where('title', 'like', "%$search%" )->paginate(10);
both of the queries work if I want to search for them individually.
Query attempt
Below is the query I've written to allow a user to search for the title of a goal and the name of a category.
$myGoals = $team->goals();
$goals = $myGoals
->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->where('title', 'like', "%$search%" )->paginate(10);
I have tried the above query but the search results return empty when I enter an input. I've also tried swapping the where() to orWhere() which then returns a goal that doesn't belong to the $team model instance but it's a goal record linking to a different Team model id within the pivot table.
Can anyone spot where I am going wrong? thanks
You need to use it all inside where.
$goals = $myGoals->where(function ($query) use ($search){
return $query->whereHas('categories', function ($q) use ($search) {
$q->where('name', 'like', "%$search%");
})->orWhere('title', 'like', "%$search%" );
})->paginate(10);
The items have category_id and location_id which has relation to tables category and location.
The search form has 3 fields, 1 item keyword, 2nd is the category selection and 3rd is its location.
The user has to fill all the fields in order to be able to search.
The problem is that the search works only for the first input field and brings all the items with the same name but location and categories are not filtered during search...
public function search(Request $request) {
$query = $request->input('query');
$cQuery = $request->input('subCategoryId');
$pQuery = $request->input('province');
$subCategories = Business::where('name', 'like', "%$query%")
->where('province_id', 'like', "%$pQuery%")
->where('sub_category_id', 'like', "%$cQuery%")->get();
return view('pages.search-result')
->with('subCategories', $subCategories);
}
I have assumed that you have the relations setup for all the models. If not, please go through Defining Relationships.
Consider the below answer as a skeleton example for what you are looking for.
$businesses = Business::where('name', 'like', '%' . $query . '%')
->with('province', 'subCategory')
->whereHas('province', function ($q) use ($pQuery) {
$q->where('name', 'like', '%' . $pQuery . '%');
})
->whereHas('subCategory', function ($q) use ($cQuery) {
$q->where('name', 'like', '%' . $cQuery . '%');
})
->get();
I did a search for a Model (Startup), next step is to order results of the search by DESC or ASC (it doesn't matter now)
My current code is:
$startups = Startup::whereHas('category', function ($query) use ($search, $sort_type) {
$query
->where('name', 'like', "%$search%")
->orderBy('name', $sort_type);
})
->orWhere('description', 'LIKE', "%$search%")
->orWhere('url', 'LIKE', "%$search%")
->get();
return StartupResource::collection($startups);
Explanation of the code:
as you see at the beginning I'm using "whereHas" to search also for coincidences in related model - "Category".
then inside of "whereHas" I'm trying to apply 'orderBy('name', $sort_type)' but it doesn't work properly (it doesn't sort by categories)
I know that we can create method in Startup model and sort it inside of the method, but the problem is that I have to pass variables to the method ( $sort_type, $search) and I don't know how to do this
So how to sort Startups model including related Category model by ASC or DESC in my case ?
Thank you guys a lot for any ideas and help!
That's not possible with whereHas(). You can use a JOIN:
$startups = Startup::select('startups.*')
->join('category', 'category.id', '=', 'startups.category_id')
->where('category.name', 'LIKE', "%$search%")
->orWhere('startups.description', 'LIKE', "%$search%")
->orWhere('startups.url', 'LIKE', "%$search%")
->orderBy('category.name', $sort_type)
->get();
I have models: Student, Tutor, Country.
Main model is Student with code:
public function studentTutors()
{
return $this->morphedByMany(Tutor::class, 'studentable')
->with('tutorAddresses');
}
Then relations.
Tutor:
public function tutorAddresses()
{
return $this->hasMany(TutorAddress::class, 'tutor_id', 'id')
->with('tutorCountry');
}
TutorAddress:
public function tutorCountry()
{
return $this->hasOne(Country::class, 'country_id', 'country_id')
->where('user_lang', 'en');
}
How do I use it:
$paginator = $student->studentFavouriteTutors()
->getQuery() //for paginate
->where(function ($query) use ($searchPhraze) {
if (strlen(trim($searchPhraze))) {
return $query
->where('username', 'like', '%' . $searchPhraze . '%')
->orWhere('firstname', 'like', '%' . $searchPhraze . '%')
->orWhere('lastname', 'like', '%' . $searchPhraze . '%');
}
})
->paginate($pages, $columns, $pageName, $page);
Question:
I am searching in tutors table (Tutor) for user/first/last names.
Is there are way to search for country name from countries table (Country: tutorCountry)? Lets say, table has 'name' column with country names.
If yes, how should $paginator code look like, to get data from countries table?
Same question goes for relation tutorAddresses. Lets say, table has 'city' column with city names.
Is this possible?
Now, I do not use relations for search, and just do joins.
BTW: I tried hasManyThrough relation, but it does not seem to pass data from that 'through' table, so this is not going to work for me. Also, my 'through' relations go a bit too deep for it (unless I do not understand something as far as this relation is concerned).
EDIT:
Answer by jedrzej.kurylo is perfect!
I just want to add, for all these, who look for a way to search within relation of a relation, like in my case:
studentTutors / tutorAddresses / tutorCountry
... where within model Student, I also want to look for country name inside of Country model, that is deeper in chain of relations and is not directly related to Tutor, but to TutorAddress, which is related to Tutor.
It is just a question of nesting queries:
$searchPhraze = 'France';
$res = $student->studentFavouriteTutors()
//first relation level
->whereHas('tutorAddresses', function($query) use ($searchPhraze) {
//deeper relation
$query->whereHas('tutorCountry', function($query) use ($searchPhraze) {
$query->where('country', 'like', '%' . $searchPhraze . '%');
});
})->get();
Or you can even combine of searches of parent and child relations:
$searchPhraze = 'Hodkiewiczville';
$res = $student->studentFavouriteTutors()
//first relation level
->whereHas('tutorAddresses', function($query) use ($searchPhraze) {
$query
//first level relation search
->where('city', 'like', '%' . $searchPhraze . '%')
//deeper relation
->orWhereHas('tutorCountry', function($query) use ($searchPhraze) {
$query->where('country_label', 'like', '%' . $searchPhraze . '%'));
});
})->get();
Above code found related datasets as expected.
Thou, I am not sure as to benchmark of this.
You can search data in related tables using whereHas() function, e.g.:
$student->studentFavouriteTutors()
->whereHas('tutorAddresses', function($query) use ($searchPhraze) {
$query->where('city', 'like', '%' . $searchPhraze . '%');
})->get();
This will get you all student's favourite tutors that have an address where city column contains given phrase.
I have this search query for Post model in Laravel:
return $q ->where('title', 'like', '%'.$keyword.'%')
Which works totally fine. In my Post model I also have a relation database categories. This is available as an object in my returned post object.
Is there a way I can query the category -> name?
I have tried:
return $q ->where('category->name', 'like', '%'.$keyword.'%')
I believe what you are looking for can be found here: https://laravel.com/docs/5.6/eloquent-relationships#querying-relations
For your use case you need to make sure you have the relationship established in your Post model. If that is set your query should look similar to the following:
return $q->where('title', 'like', '%'.$keyword.'%')->whereHas('category',
function ($query) use ($keyword) {
$query->where('name', 'like', '%'.$keyword.'%');
})->get();
Hope this helps!
If you're db is well structured - then will want to use Laravel's ORM for this stuff.
https://laravel.com/docs/5.6/eloquent#introduction
I assume that you have Models:
Post
Category
Also that you have category_id as a foreign key in Post model.
This is basic structure.
I assume that you want to search Posts and Categories same time so you can do something like this:
return Post::leftJoin('categories', 'posts.category_id', '=', 'categories.id)
->where('categories.name', 'like', '%' . $keyword . '%')
->orWhere('posts.title', 'like', '%' . $keyword . '%')
->get();