How can I do a Like-query, with multiple values to search for?
$searchWords = explode(' ', Input::get('search'));
Then I get an array of words that were given for the search.
How can I pass it in this:
$pages = Page::where('content', 'LIKE', '%'.$singleWord.'%')->distinct()->get();
A loop can't work, then it overwrites the $pages always; then it keeps always the latest search:
foreach($searchWords as $word){
$pages = Page::where('content', 'LIKE', '%'.$word.'%')->distinct()->get();
}
A loop is the solution but you only need to add the where condition without executing the query
$pages = Page::query();
foreach($searchWords as $word){
$pages->orWhere('content', 'LIKE', '%'.$word.'%');
}
$pages = $pages->distinct()->get();
i will code it for you :
$pages = Page->where(function($query) use($word){
foreach($searchWords as $word){
$query->orWhere('content', 'LIKE', '%'.$word.'%');
}
})->get();
you can try that , i hope that will help :)
Related
i want to search data based nama_kegiatan or tanggal_kegiatan where these two name form my table field, this code from my controller :
public function search(Request $request){
$cari = $request->search;
$caritanggal = date($request->datekeg);
$infokeg = Infokeg::where(function ($query) use ($cari) {
$query->where('nama_kegiatan','like',"%$cari%")->get();
})->orWhere(function($query) use ($caritanggal) {
$query->where('tanggal_kegiatan', $caritanggal)->get();;
})->paginate(10);
return view('infokeg/infokeg', ['infokeg' => $infokeg]);
}
when i am using like on nama_kegiatan, tanggal_kegiatan didnt work and just display all data, but when i delete like from nama_kegiatan, its work, but i cant search nama_kegiatan using like, so how to solve it ?
Maybe you can try to get() all the result after the query? Somethings like
$infokeg = Infokeg::where('name_kegiatan','LIKE','%'.$cari.'%')->orWhere('tanggal_kegiatan',$caritanggal)->get();
IMO the get() and paginate() cannot be use at the same time if I have not get it wrong. If you need to paginate after all the result, you can do create Paginator manually. Maybe this link can help you. Hope this will help you and please correct me if anything wrong. Thanks!
If you want to see what is run in the database use dd(DB::getQueryLog()) after query to see what queries exactly were run.
$infokeg = Infokeg::where('nama_kegiatan','like', "%" . $cari ."%")
->orWhere(function($query) use ($caritanggal) {
$query->where('tanggal_kegiatan', $caritanggal);
})->paginate(10);
just make it into if condition
public function search(Request $request)
{
$cari = $request->search;
$caritanggal = $request->datekeg;
if (isset($cari)) {
$infokeg = Infokeg::where('nama_kegiatan', 'like', '%'.$cari.'%')
->paginate(5);
}elseif (isset($caritanggal)) {
$infokeg = Infokeg::where('tanggal_kegiatan', 'like', '%'.$caritanggal.'%')
->paginate(5);
}elseif (isset($cari)&&isset($caritanggal)) {
$infokeg = Infokeg::where('nama_kegiatan', 'like', '%'.$cari.'%')
->orWhere('tanggal_kegiatan', 'like', '%'.$caritanggal.'%')
->paginate(5);
}
return view('/admin/infokeg/infokeg', ['infokeg' => $infokeg]);
}
I currently have:
$comments = $this->post->comments()
->where('comment', 'like', "%{$search}%")
->paginate(50);
But I'd like to do something like the below, as I have multiple filters that I'm going to apply:
$comments = $this->post->comments();
if(condition)
{
$comments->where('comment', 'like', "%{$search}%");
}
$comments->paginate();
But the code above doesn't work as I then get errors on things like $comments->links() for pagination links in the view, indicating that this doesn't work.
What you can try is something like this, I don't know what Laravel version you are using, also I don't know the structure you have of your models, but here's the documentation about this example:
$conditions = 1;
$comments = Post::with('comments')
->whereHas('comments', function ($query) use ($conditions){
// pass your conditions
if(!$conditions){
$query->where('comment', 'like', "%%")
}
// more conditions, etc
$query->where('comment_id', 'like', '%%');
})->paginate(50);
It was missing $comment = in front of the query clauses:
$comments = $this->post->comments();
if(condition)
{
$comments = $comments->where('comment', 'like', "%{$search}%");
}
$comments = $comments->paginate();
I am using eloquent query to filter data. My query is like this and it is working perfectly for now.
$users = User::where('username','LIKE','%'.request('username').'%')
->where('first_name','LIKE','%'.request('first_name').'%')
->where('last_name','LIKE','%'.request('last_name').'%')
->where('gender','LIKE','%'.request('gender').'%')
->where('password','LIKE','%'.request('password').'%')
->SimplePaginate(15);
My request data was like this.
However I need to update this query for dynamic fields. There can be different fields. What I did was to send the request in an associative array. So, my request data turned into like this,
What I intend to do is to put all request data in a search array. Then use a loop to run like the above query. How can I do that?
P.S. I checked in the forum and found some similar questions. Some of them are outdated. Others didn't solve my problem.
If I understand you right you can do like this:
$search = request('search', []);
$users = User::query();
foreach($search as $field=>$value)
{
$users = $users->where($field,'LIKE','%'.$value.'%');
}
$users = $users->SimplePaginate(15);
You can try this.
$search = $request->get('search');
User::where(function ($q) use ($search){
foreach ($search as $key=>$value) {
if($value != null ){
$q->where($key, 'like', "%{$value}%");
}
}
})->get();
The where() clause can also accept an array. With that in mind, you could map over your search parameters and convert it to the format you want and then feed it into the query.
For example:
$search = [
'first_name' => 'John',
'last_name' => 'Smith'
];
$filters = collect($search)->map(function($value, $key) {
return [$key, 'LIKE', "%{$value}%"];
})->values()->toArray();
return User::where($filters)->SimplePaginate(15);
I have saved all of my where clause in a variable and i'm trying to join that variable inside laravel query but its not working. Here is mine code
$where .= "";
$where .= '->where("product_details.title", '.$request->title.')';
$where .= '->where("product_details.id", '.$request->id.')';
$results = DB::table('product_details').'$where'.->get();
How to use variable inside query?
you can used like that by using if condition statement for dynamic query
$results = DB::table('product_details')
if($request->title) {
$results->where("product_details.title", $request->title);
}
if($request->id) {
$results->where("product_details.id", $request->id);
}
$result->get();
I am not sure why you want to do this. The following should just work.
DB::table('product_details')
->where([
'product_details.title' => $request->title
'product_details.id' => $request->id
])
->get();
Now, let's say that you may not always have a title or id from the request, you could also do this
DB::table('product_details')
->when($request->title, function ($query, $title) {
return $query->where('product_details.title', $title);
})
->when($request->id, function ($query, $id) {
return $query->where('product_details.id', $id);
})
->get();
this will not work because you are saving text in string and output will be on string you need to convert it to eval Try This.
$results = DB::table('product_details') . eval ($where) .->get();
I have a search button and pagination system. They are working perfectly. But when i searched something and page was paginated with numbers. After clicking next page. Search query is gone. After that all values are coming into page2. Not searched values.
My search button changing url like this /en/news?q=foo
My pagination is changing url like this(default btw) /en/news?page=2
How can i add this side by side. Or how can i solve the problem. I am open any solution.
I am using scope for search.
public function scopeSearch($query, $search) {
return $query->where('title', 'LIKE', '%' .$search. '%')
->orWhere('sub_body', 'like', '%' .$search. '%')
->orWhere('body', 'like', '%' .$search. '%');
}
Also this is my controller:
public function index (Request $request){
$localeCode = LaravelLocalization::getCurrentLocale();
$updates = Update::latest()->take(3)->get();
$query = $request->get('q');
if ($query){
$new = $query ? News::search($query)->orderBy('id', 'desc')->paginate(10):News::all();
return view('frontend.news', compact('localeCode', 'updates', 'new', 'query'));
}
else
{
$new = News::orderBy('id', 'desc')->paginate(10);
return view('frontend.news', compact('localeCode', 'updates', 'new', 'query'));
}
}
I hope i can express my own.
{{ $users->appends($_GET)->links() }}
that append all your filter value in one line
It is explained in the documentation for displaying pagination results:
Appending To Pagination Links
You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:
{{ $users->appends(['sort' => 'votes'])->links() }}
I find your current code quite confusing. I think you can rewrite it like this
public function index (Request $request){
$localeCode = LaravelLocalization::getCurrentLocale();
$updates = Update::latest()->take(3)->get();
$query = $request->get('q');
$new = $request->filled('q') ? News::search($query) : News::query();
$new = $new->orderBy('id', 'desc')->paginate(10);
return view('frontend.news', compact('localeCode', 'updates', 'new', 'query'));
}
and in the view you'll have to use
{{ $new->appends(['q' => $query])->links() }}
Try to use {{ $collection->appends(request()->all())->links() }}