How to search from multiple tables in laravel? - php

How can I search from multiple tables using laravel eloquent. below is my query.
$data= User::where('id',$search_str)
->orWhere('name', 'LIKE', "%{$search_str}%")
->with('usersecond:id,name,email')->get();
If I search for something I get results from the user table. how can I get results from usersecond table in this query?

use whereHas() function
$data = User::where('id', $search_str)
->orWhere('name', 'LIKE', "%{$search_str}%")
->with('usersecond:id,name,email')
->orWhereHas('usersecond', function ($q) use ($search_str) {
$q->where('name', 'LIKE', "%{$search_str}%");
})->get();
ref link https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-existence

Related

Joining two models in Laravel Eloquent

I have a model stored in a variable like:
$user = User::where('name', 'like', $test)
->orderBy('name');
I would like to build another query where I can join $user. Can't find the right syntax to do it.
What am trying to achieve:
$numbers= Number::join($user, $users.id, '=', 'number.user_id')
->where('name', 'like', "%" . $validated['text'] . "%")])
->get();
Assuming you have typical hasMany/belongsTo relationships set up between User and Number models, this should work:
User::where("name", "like", $test)
->whereHas("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->with("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->get();
The where() method, of course, matches users with the desired name. The whereHas() method further restricts based on the relationship, looking only for users having numbers with a matching name. Assuming you want to retrieve those matching numbers, you have to do the same filter again on the eager load.
Try this,
$numbers= Number::whereHas('<Your Eloquent Model>',function($query)use($validated){
$query->where('name', 'like', "%" . $validated['text'] . "%")]);
}
->get();
Join can be written in this way
$records = User::select('users.*')
->where('users.name', 'like', $test)
->join('numbers', function($join) {
$join->on('users.id', '=', 'numbers.id')
->where('numbers.name', 'like', "%" . $validated['text'] . "%");
})
->get();

Laravel where clause is not returning data after multiple orWhere clauses?

I am using laravel 6.10 version in that I am implementing search,
I have 3 tables
1) course_category
2) course_sub_category
3) course [course_id_category(foreign key),course_sub_category_id(foreign key)]
below is my code
$course = $request->searchItem;
if ($course=="") {
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->orderBy('course.title','asc')
->paginate(15);
}
else{
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where('course.title', 'LIKE', '%'.$course.'%')
->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
->get();
}
when i am retunring the values i am gatting 0 arrays but when i am removing one orwhere from existing my query is working and its returnig we all values with match
means when i am using multiple orWhere in laravel my where is not working, please share the solution on same.
->orWhere doesnt work like typical SQL. You should use it like that:
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where(function($query) use ($course){
$query->where('course.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
})
->get();

I want to get all images which either have a word in their name, as one of their tags or as category

I'm trying to add search functionality to my website and have been tinkering with the eloquent queries I'd need to execute. Currently I've made 3 queries and each gets images meeting a certain criteria, however, I'm not sure how to combine it into 1 query that would spit all images that meet one, two or all criteria.
$images = Image::where('name', 'like', '%'.$query.'%')->get();
This query gets all images that have a name similar to the searched word.
$images = Image::whereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
This query gets all images that have a tag similar to the searched word.
$images = Image::whereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
And finally, this query gets all images that have a category similar to the searched word.
public function search($query){
$images = Image::where('name', 'like', '%'.$query.'%')->get();
$images = Image::whereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
$images = Image::whereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orderBy('created_at', 'desc')->get();
return view('search', ['images' => $images]);
}
Is this the correct way to create a search functionality? Is there anything I could do to enhance it further? Is there some obvious problem that I personally might be overseeing? I'd appreciate any tips and tricks since I believe the search functionality is important for CRUD applications like mine.
Merge the constraint into a single query:
$images = Image::where('name', 'like', '%'.$query.'%')
->orWhereHas('tags', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->orWhereHas('category', function($q) use ($query) {
return $q->where('name', 'like', '%'.$query.'%');
})->latest()
->get();
latest() is equivalent to orderBy('created_at', 'desc').

Laravel Query relationships

I have query with this relationships
$query = ExtraDay::with([
'credit.client'
]);
Now i am going to make searchfield filter for 'name' field which is in CLIENT model. ExtraDay model is connected to Client model via Credit model. Below there is a way how to make search for one level relationship like with(['client']);
$query->where('name', 'like', "%$q%")->whereHas('client', function
($organization) use ($q) {
$organization->where('name', 'like', "%$q%")
->orWhere('short_name', 'like', "%$q%");
But i have with(['credit.client']); How to search field 'name' in this situation?
whereHas works with nested relations, so use the same dot notation credit.client. For example:
ExtraDay::whereHas('credit.client', function ($client) use ($q) {
$client->where('name', 'like', "%$q%");
})->get();

Laravel Eloquent Search Multiple Related Tables

I am struggling with figuring out how to run a like query against multiple related tables.
I have a submissions table that has related users, mcd_forms, and submission_statuses tables.
Here is my code for running a LIKE statement with the given $terms_like.
$submission = new Submission;
$terms_like = '%'.$search_terms.'%';
$data['submissions'] = $submission
->join('users as users', 'users.id', '=', 'submissions.user_id')
->join('mcd_forms as forms', 'forms.submission_id', '=', 'submissions.id')
->join('submission_statuses as statuses', 'statuses.id', '=', 'submissions.submission_status_id')
->where(function($q) use ($terms_like) {
$q->where('users.user_group_id', '=', Auth::user()->user_group_id)
->orWhere('forms.name', 'like', $terms_like)
->orWhere('forms.custom_id', 'like', $terms_like)
->orWhere('forms.start_date', 'like', $terms_like)
->orWhere('forms.end_date', 'like', $terms_like)
->orWhere('forms.soft_sell_date', 'like', $terms_like)
->orWhere('forms.region', 'like', $terms_like)
->orWhere('statuses.status_formatted', 'like', $terms_like);
});
No matter what I try it returns incorrect results. What am I doing wrong?
In your query above, since you are not using the "%" symbol, your like clause is working as an "=" since it's trying to match the whole word.
Replace all the "where" clause like this:
->orWhere('forms.name', 'like', "%".$terms_like."%")
This will try to match the word anywhere in the text.
You can try 'like' operator following this:
$users = DB::table('users')
->where('forms.name','LIKE', '%'.$terms_like.'%')
->get();

Categories