Laravel 5 How to use multi pagination in one page - laravel-5

Let's say I have a index page which display all the data in database with pagination. I am able to display the correct data and pagination after users perform search, but when I click on the second page, it's not showing the correct data.
public function getNewsletterSearch(Request $request) {
$keyword = $request->news_search;
$searchData = DB::table('tbl_contents')
->join('tbl_files', function ($join) use($keyword) {
$join->on('tbl_contents.unique', '=', 'tbl_files.unique')
->where('tbl_contents.record_type', '=', 'Newsletter')
->where('tbl_contents.title', 'LIKE', '%' . $keyword . '%')
->where('tbl_files.order_id', '=', 1);
})
->orderBy('tbl_contents.id', 'DESC')
->select('tbl_contents.*', 'tbl_files.file_name')
->paginate(6);
$searchData->setPageName('page_result');
return view('newsletter.index')->with(['searchData' => $searchData])->with('search', $keyword);
}
Here is the view:
<div class = "col-xs-12">
<nav aria-label = "Page navigation">
{{with(new App\Pagination\CustomPresenter($searchData->appends('page_result', Illuminate\Support\Facades\Input::get('page_result'))))->render()}}
</nav>
</div>
This is the search function i created. Can I know what is the solution to display the correct data on page 2 from the search result?

Here is the complete index function which, i had used in my search scenario, take a look and do the necessary modification.
public function getNewsletterSearch()
{
$keyword = request()->get('news_search');
$searchData = DB::table('tbl_contents')
->join('tbl_files', function ($join) use($keyword) {
$join->on('tbl_contents.unique', '=', 'tbl_files.unique')
->where('tbl_contents.record_type', '=', 'Newsletter')
->where('tbl_contents.title', 'LIKE', '%' . $keyword . '%')
->where('tbl_files.order_id', '=', 1);
})
->orderBy('tbl_contents.id', 'DESC')
->select('tbl_contents.*', 'tbl_files.file_name');
$searchData= $searchData->paginate(6)->appends(request()->query());
return view('category.index',compact('searchData'));
}
in view file
<div class="col-sm-12 col-md-12"><span class="pull-right">{{$searchData->links()}}</span></div>

Related

How can I render search on Livewire with an orWhere clause not related to search?

I have here a Livewire render function that displays records depending on what is searched on the textbox. It was working fine, and giving me results in an autocomplete manner. However, when I added this line ->orWhere('qty_on_hand', '!=', 0), it stops that autocomplete searching.
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Product;
class LoadInStockProducts extends Component
{
public $searchTerm;
public $amount = 10;
protected $listeners = [
'load-more' => 'loadMore'
];
public function loadMore()
{
$this->amount = $this->amount + 10;
}
public function render()
{
$searchTerm = '%' . $this->searchTerm . '%';
$products = Product::orderBy('name')->where('code', 'like', $searchTerm)
->orWhere('name', 'like', $searchTerm)
->orWhere('description', 'like', $searchTerm)
->orWhere('qty_on_hand', '!=', 0)
->paginate($this->amount);
$this->emit('productStore');
return view('livewire.load-in-stock-products', ['products' => $products]);
}
}
How can I make the autocomplete searching work, even with the ->orWhere('qty_on_hand', '!=', 0) condition? Also, another question, I've tried this ->orWhere('srp', '!=', 0.00) and it's not working. How can I make it work for float types? The srp field is a float type by the way.
Any help is much appreciated.
It sounds to me like you want to produce a query where you search for the given fields, and only show the records that has a quantity on hand - that would look like this in raw SQL,
SELECT *
FROM products
WHERE (
code LIKE '%searchterm%'
OR name LIKE '%searchterm%'
OR description LIKE '%searchterm%'
)
AND qty_on_hand != 0
ORDER BY name
Note how the search for code/name/description is within its own group, and that you look for any match of either of those and where the quantity is different from zero.
In Laravel, that means you have to group the query as well, using a closure.
$products = Product::where(function($query) use ($searchTerm) {
return $query->where('code', 'like', $searchTerm)
->orWhere('name', 'like', $searchTerm)
->orWhere('description', 'like', $searchTerm);
})
->where('qty_on_hand', '!=', 0)
->orderBy('name')
->paginate($this->amount);
You can introduce more groups if you have other conditions, like having two where() with a closure, where the queries inside use orwhere().
$products = Product::where(function($query) use ($searchTerm) {
return $query->where('code', 'like', $searchTerm)
->orWhere('name', 'like', $searchTerm)
->orWhere('description', 'like', $searchTerm);
})
->where(function($query) {
return $query->where('qty_on_hand', '!=', 0)
->orWhere('srp', '!=', 0);
})
->orderBy('name')
->paginate($this->amount);

How to search an item by category and location in laravel simultaneously

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();

Laravel dynamic query

I have a GET form with three filters.
make
Year
country
I need to get all posts from db. But filter the results based on these three filters.
If a country is selected, get posts for that country only or all countries.
if a make is selected, get posts for that make only or all makes
if a year is selected, get posts for that year only or all years
how to write one query that filters all these three options. What I have done is used if and else statements and written different queries for each scenario. That's 9 queries to get one information. Can we make it dynamic and just have one query?
My Example query:
public function search(Request $request)
{
$search=$request->input('search');
if($request->input('country') == "all")
{
$posts = Post::where('status','Published')->orderBy('status_change','DESC')
->where('status','Published')
->where(function($query) use ($search){
$query->where('title','LIKE','%'.$search.'%');
$query->orWhere('model','LIKE','%'.$search.'%');
$query->orWhere('notes','LIKE','%'.$search.'%');
$query->orWhere('description','LIKE','%'.$search.'%');
})
->paginate(25);
}
else
{
$posts = Country::where('country_name', $request->input('country'))->first()->posts()->orderBy('status_change','DESC')
->where('status','Published')
->where(function($query) use ($search){
$query->where('title','LIKE','%'.$search.'%');
$query->orWhere('model','LIKE','%'.$search.'%');
$query->orWhere('notes','LIKE','%'.$search.'%');
$query->orWhere('description','LIKE','%'.$search.'%');
})
->paginate(25);
}
return view('welcome')
->with('published_posts',$posts)
;
}
I think something like this would work:
/**
* #param Request $request
*/
function search(Request $request)
{
$postsQuery = Post::where('status', 'Published');
if ($request->has('country')) {
$country = $request->country;
// assuming relationships are setup correclty
$postsQuery->whereHas('country', function ($query) use ($country) {
$query->where('country_name', 'LIKE', $country);
});
}
if ($request->has('search')) {
$postsQuery->where(function ($query) use ($search) {
$query->where('title', 'LIKE', '%' . $request->search . '%');
$query->orWhere('model', 'LIKE', '%' . $request->search . '%');
$query->orWhere('notes', 'LIKE', '%' . $request->search . '%');
$query->orWhere('description', 'LIKE', '%' . $request->search . '%');
});
}
$postsQuery->orderBy('status_change', 'DESC')->paginate(25);
return view('welcome')->with('published_posts', $result);
}
I used 'when' method.
$make = null;
$year = null;
$country = null;
if($request->filled('make')){
$make = $request->query('make');
}
if($request->filled('year')){
$year = $request->query('year');
}
if($request->filled('country')){
$country = $request->query('country');
}
$posts = DB::table('posts')
->when($make, function($query, $make){
return $query->where("make", "=", $make);
})
->when($year, function($query, $year){
return $query->whereYear("year", "=", $year);
})
->when($country, function($query, $country){
return $query->where('country', "like", $country);
})
->get();
Check out the Laravel Docs:
Check out an article here

Laravel search with localization

I have a scope about searcing. Also using localization from mcamara
Right now, when i am search something, results are coming with different languages. But i can just search with my main language. After results are came translation happening. This is related with my blade.
{!! $news->translate($localeCode)->title !!}
But main question is before the search. How can i search something with selected language. I can switch language easily. This is my search scope.
public function scopeSearch($query, $search) {
$localeCode = LaravelLocalization::getCurrentLocale();
return $query->translate($localeCode)->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');
$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'));
}
I'll be glad if anyone help. Thanks advance.

query search LIKE use keywords where and left join LARAVEL 5

I want to find a data from two tables based on the name contained in the users, but I am having problems.
This is the users table containing the id, and name.
This is the student table that contains the id, and user_id.
Where user_id student = id users.
public function search(Request $request)
{
$keywords = trim($request->input('keywords'));
if (!empty($keywords)) {
$id_class = $request->input('id_class');
//Query
$query = User()
->leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
(!empty($id_class)) ? $query->Kelas($id_class) : '';
$data_student = $query->paginate(10);
//URL link pagination
$pagination = (!empty($id_class)) ? $pagination = $data_student->appends(['id_class' => $id_class]) : '';
$pagination = $data_student->appends(['keywords' => $keywords]);
$amount_data = $=data_student->total();
return view('student.index', compact('data_student', 'keywords', 'pagination', 'amount_data', 'id_class'));
}
return redirect('student');
}
Call to undefined function App\Http\Controllers\User()
As mentioned previously you need to correctly refer to the User class with namespace, you also need to change thew following:
$query = User()
->leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
to:
$query = User::leftjoin('student', 'users.id', '=', 'student.user_id')
->where('users.name', 'LIKE', '%' . $keywords . '%');
You must call the first method on the class as static with ::.
Also another potential typo you join the table student should it be plural; students?

Resources