I wish to make a custom search on my site.
Here is MySQL Search:
SELECT * FROM MyDB.MyTable WHERE (id LIKE '%MySearch%' OR firstname LIKE '%MySearch%' OR lastname LIKE '%MySearch%' OR email LIKE '%MySearch%' OR address LIKE '%MySearch%');
How can I get that search into my Laravel Controller??
$users = DB::table('MyTable')->where('firstname', 'MySearch')->get();
My Laravel Controller
Thanks in advance !
$users = DB::table('MyTable')
->where('id', 'LIKE', '%MySearch%')
->or_where('firstname', 'LIKE', '%MySearch%')
->or_where('lastname', 'LIKE', '%MySearch%')
->or_where('email', 'LIKE', '%MySearch%')
->or_where('address', 'LIKE', '%MySearch%');
->get();
for laravel 5.2:
$search = '%'.$_POST['name'].'%';
$users = DB::table('MyTable')
->where('id', 'LIKE', $search)
->orwhere('firstname', 'LIKE', $search)
->orwhere('lastname', 'LIKE', $search)
->orwhere('email', 'LIKE', $search)
->orwhere('address', 'LIKE', $search);
->get();
It work !
You can build queries with 'orwhere' like bellow
$users = DB::table('users')
->where('firstname', 'like', 'John%')
->orWhere('lastname', 'John')
->get();
Please refer to this document before building your queries.
https://laravel.com/docs/5.2/queries
This one also works for me in laravel 5.2
$search = '%'.$request->search.'%';
$result = DB::table('tours')->where('name', 'like', $search)->get();
Related
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 try to search first_name, last_name and city but city is now always set whether the users want to set it with city or not. so I make query like this if request has city then filter it
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
})
->paginate(10);
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
but where location like its like never used even request has city. i've try var_dump $users and count it and end up with no city query
Do little change in sequence of your lines of code:
$users = \App\User::join('profiles', 'users.id', '=', 'profiles.member_id')
->where(function($query) {
$query->where('first_name', 'like', '%z%')
->orWhere('last_name', 'like', '%z%');
});
if ($request->has('city')) {
$users->where('location', 'like', '%bandung%');
}
$users_data = $users->paginate(10);
The variable $users_data will be having proper result.
I am using larvel eloquent.
i am using this query using model. My code is
$books = Book::select('title', 'author', Book::raw('count(*) as copies'))
->where('title', 'like', '%'.$name.'%')
->orWhere(function ($query) use ($name) {
$query->where('author', 'like', '%'.$name.'%')
->where('subject', 'like', '%'.$name.'%');
})
->groupBy('title','author')
->get();
I got the error
"strtolower() expects parameter 1 to be string, object given"
I know the error is in count(). The code
raw('count() as copies') is used in table.
But I dont know how to use count(*) as copies in model eloquent. My model name is Book.
One more doubt i have, can we use multple fields in groupBy, ie
groupBy('title','author')
use selectRaw()
$books = Book::select('title', 'author'))
->where('title', 'like', '%'.$name.'%')
->orWhere(function ($query) use ($name) {
$query->where('author', 'like', '%'.$name.'%')
->where('subject', 'like', '%'.$name.'%');
})
->selectRaw('count(*) as copies')
->groupBy('title','author')
->get();
Try this, SelectRaw
$books = Book::select('title', 'author')
->selectRaw('count(*) as copies')
->where('title', 'like', '%'.$name.'%')
->orWhere(function ($query) use ($name) {
$query->where('author', 'like', '%'.$name.'%')
->where('subject', 'like', '%'.$name.'%');
})
->groupBy('title','author')
->get();
You have this posiblity also
->select('title','author', DB::raw('count(*) as copies'))
Laravel Documentation Raw
How can I write a Laravel 5 Eloquent query like this SQL?
where ( (table1.fname like %xxxxx% )
OR (table1.lname like %xxxxx%) )
AND table1.is_active != 0
I have already tried
->where('users.is_active', '!=', 2)
->where('users.name', 'like' , '%'. $search .'%')
->orWhere('users.lname', 'like' , '%'. $search .'%')
->orWhere('users.email', 'like' , '%'. $search .'%') ->get();
Take a look at the Advanced join Clauses in Laravel Documentation
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
joins
EDIT
I think i misunderstood the question. To do nested wheres you can do the following thing:
\DB::table('table1')->...->where(function($query) {
$query->where('table1.fname', 'like', '%xxxxx$')->orWhere('table1.lname', 'like', '%xxxx%);
})->where('table1.is_active', '!=', 0);
You can use Model for this:
TableModel::where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
$data = DB::table('table1')->where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
//add use DB; in your controller
To have grouped sub-clauses that should evaluate as a whole, you can pass where() a closure. Then you want a second where call for the check on the is_active column:
$value = '%xxxxx%';
$collection = Model::where(function ($models) use ($value) {
$models->where('fname', 'like', $value)
->orWhere('lname', 'like', $value);
})
->where('is_active', '!=', 0)
->get();
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();