Knp Pagination in symfony 5 - php

I installed "Knp Paginator" and everything works fine but if i try to do a search in the search bar (this search bar does not depend on Knp Paginator) i have this error :
"Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension::render(): Argument #2 ($pagination) must be of type Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface, array given, called in... /var/cache/dev/twig/36/36da62d992e743004744882a10f47b6d89340c107d735e823c151b7c459ca09f.php on line 214"
=>see image.
If i disable Knp Paginator, the search bar work again.
Thank you for your help.
this is my queryBuilder of search bar (ItemRepository):
public function findBySearch($search)
{
return $this->createQueryBuilder('a')
->andWhere('a.city LIKE :val')
->setParameter('val', '%' . $search . '%')
->orWhere('a.title LIKE :title')
->setParameter('title', '%' . $search . '%')
->orWhere('a.city LIKE :city')
->setParameter('city', '%' . $search . '%')
->orWhere('a.zipCode LIKE :zip')
->setParameter('zip', '%' . $search . '%')
->orWhere('a.hiddenDetail LIKE :hiddenDetail')
->setParameter('hiddenDetail', '%' . $search . '%')
->getQuery()
->getResult();
}
Controller (searchBar) :
/**
*#Route("/searchItem", name="searchItem")
*
*/
public function searchItem(Request $request, ItemRepository $itemRepository)
{
$search = $request->request->get('search');
$items = $itemRepository->findBySearch($search);
return $this->render('home/listItem.html.twig', [
'items' => $items
]);
}
Controller (Knp Paginator) :
public function listItem(ItemRepository $repository, PaginatorInterface $paginator, Request $request): Response
{
$data = $repository->findAll();
// PAGINANTION KNP/PAGINATOR
$items = $paginator->paginate(
$data,
$request->query->getInt('page', 1),
8
);
return $this->render('home/listItem.html.twig', [
'items' => $items,
]);
}

KNP is receiving an array from your findAll() function. You need to write a custom repo function to return the query builder itself, and KNP will complete the query for you with any extra necessities.
see https://symfonycasts.com/screencast/symfony4-doctrine-relations/pagination
It was fruitful in my endeavors.
Edit: Also, I would take another look at your andWhere()'s. You can set one parameter for all of them at once, and just reuse the search term, if that would work for your situation.
public function findBySearch($search)
{
return $this->createQueryBuilder('a')
->andWhere('
a.city LIKE :val
OR a.title LIKE :val
OR a.city LIKE :val
OR a.zipCode LIKE :val
OR a.hiddenDetail LIKE :val')
->setParameter('val', '%'.$search.'%')
->getQuery()
->getResult();
// if you want to use the paginator, leave these last two
// lines off. Return just the qb.
}

Related

Laravel Query Builder Join vs With Function, Which One is Better?

I want to ask about Laravel Query using Join or With which is better.
In this case there is a short query that I have tried. But there are some things that make me wonder.
In my case, I'm trying to create a list of users using the API. The problem lies in sorting the data.
The problem is divided into several.
If I use With.
The advantage of using with is that I can call the attributes in the model without rewriting the attributes I want to use. But I was confused when calling data related to other tables for me to sort. example query:
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$sortBy = $request->query('sortBy');
$sortDesc = (is_null($request->query('sortDesc'))) ? $request->query('sortDesc') : ($request->query('sortDesc') == 'true' ? 'desc' : 'asc');
$page = $request->query('page');
$itemsPerPage = $request->query('itemsPerPage');
$search = $request->query('search');
$starDate = $request->query('start');
$endDate = $request->query('end');
$start = ($page - 1) * $itemsPerPage;
$query = MemberRegular::query();
$query->with(['users' => function ($subQuery) {
$subQuery->select('id', 'name', 'email', 'phone');
}]);
$query->select(
'id',
'code'
);
if ($search) {
$query->where(function ($subQuery) use ($search) {
$subQuery->where('code', 'like', '%' . $search . '%');
$subQuery->orWhere(function ($q) use ($search) {
$q->whereHas('users', function ($j) use ($search) {
$j->where('name', 'like', '%' . $search . '%');
$j->orWhere('email', 'like', '%' . $search . '%');
})
});
});
}
if ($sortBy && $sortDesc) {
$query->orderBy($sortBy, $sortDesc)->orderBy('id', 'desc');
} else {
$query->orderBy('created_at', 'desc')->orderBy('id', 'desc');
}
if ($starDate && $endDate) {
$query->whereBetween('created_at', [$starDate, $endDate]);
}
$data['totalItems'] = $query->count();
$data['items'] = $query->skip($start)->take($itemsPerPage)->get();
return HResource::collection($data['items'])->additional(['totalItems' => (int) $data['totalItems']], true);
}
If I use Join.
The advantage of using Join is that I can sort data easily if the data is related to other tables. But I have to re-create a new attribute in a collection. example query:
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$sortBy = $request->query('sortBy');
$sortDesc = (is_null($request->query('sortDesc'))) ? $request->query('sortDesc') : ($request->query('sortDesc') == 'true' ? 'desc' : 'asc');
$page = $request->query('page');
$itemsPerPage = $request->query('itemsPerPage');
$search = $request->query('search');
$starDate = $request->query('start');
$endDate = $request->query('end');
$start = ($page - 1) * $itemsPerPage;
$query = MemberRegular::query();
$query->join('users', 'users.id', '=', 'member_regulars.user_id');
$query->select(
'member_regulars.id',
'member_regulars.code',
'users.name',
'users.email',
'users.phone'
);
if ($search) {
$query->where(function ($subQuery) use ($search) {
$subQuery->where('member_regulars.code', 'like', '%' . $search . '%');
$subQuery->orWhere('users.name', 'ilike', '%' . $search . '%');
$subQuery->orWhere('users.email', 'ilike', '%' . $search . '%');
$subQuery->orWhere('users.phone', 'ilike', '%' . $search . '%');
});
}
if ($sortBy && $sortDesc) {
$query->orderBy($sortBy, $sortDesc)->orderBy('member_regulars.id', 'desc');
} else {
$query->orderBy('member_regulars.created_at', 'desc')->orderBy('member_regulars.id', 'desc');
}
if ($starDate && $endDate) {
$query->whereBetween('member_regulars.created_at', [$starDate, $endDate]);
}
$data['totalItems'] = $query->count();
$data['items'] = $query->skip($start)->take($itemsPerPage)->get();
return HResource::collection($data['items'])->additional(['totalItems' => (int) $data['totalItems']], true);
}
If using Query With The problem lies in sending the sortBy parameter like the following users.name it will be an error because the table is not found in the query I made, but I can immediately call attributes that can be used directly without needing to create a new custom attribute.
If using Query Join, the problem is that I have to re-create custom attributes to be used in data collections, but I don't need to worry about sorting data.
Both are equally important to me. However, if anyone is willing to give advice on the best way I have to use Join or With for this case.
Thank you.
Finally I found the best solution to the problem I was facing. I hope this can help others.
Here I choose to use Join why? because it turns out that I can call the function relations users() in the model that I created so that I can still retrieve custom attributes in the Users model. I don't really know if this is the right way or not. I hope this helps others.
Thank you.

Pagination coming from AJAX request not working properly -- Laravel 5.6

I'm using Laravel 5.6 -- Jquery Ajax
Here's the point
I'm having a search input placed in my navbar there's an eventlistener('keyup') on it.
Everytime keyup is fired, an AJAX GET request is send to
url : '{{action('PostController#searchAdmin')}}'
From web.php : Route::get('/search/admin', 'PostController#searchAdmin');
I made the return of that action a partial with data
return view('back.partials.searchResult', ['posts' => $posts, 'trashed' => $trashed]);
And I replace the content of the main tag with that partial
Everything is working properly except when the result count is greater than 10 (the breakpoint of pagination).
Pagination control links are all pointing to "search/admin?page=x" and when I click on it, this error is showing
Undefined variable: posts
I used $posts->links() to show the controls
I found a solution so I post it
In web.php
Route::get('/search', function(Request $request) {
$search = $request->search;
$trashed = Post::trash()->count();
$posts = Post::notTrash()
->where('title', 'like', '%' . $search . "%")
->orWhere('post_type' , 'like', '%' . $search . '%')
->paginate(10);
$posts->withPath('?search=' . $search);
return view('back.partials.searchResult', ['posts' => $posts, 'trashed' => $trashed, 'search' => $search]);
});
This code was for test purpose and will be soon exported in a new controller called SearchController
In my PostController
public function index(Request $request)
{
// GET parameters
$paginate = $request->input('paginate') ?? 10;
$search = $request->input('search') ?? null;
if($search !== null) {
$posts = $this->checkCategories($paginate, $search);
} else {
$posts = Post::notTrash()->orderBy('id', 'ASC')->paginate($paginate);
}
$trashed = Post::trash()->count();
$posts->withPath('?search=' . $search);
return view('back.index', ['posts' => $posts, 'trashed' => $trashed, 'search' => $search]);
}
Working with
private function checkCategories($paginate, $search)
{
$categories = Category::all();
foreach ($categories as $category) {
if(strpos(strtolower($category->name), $search) === false) {
// #TODO: Change for stripos
$posts = Post::notTrash()
->where('title', 'like', '%' . $search . '%')
->orWhere('post_type', 'like', '%' . $search . '%')
->paginate($paginate);
} else {
return Category::find($category->id)->posts()->paginate($paginate);
}
}
return $posts;
}
The index method now accept Request to handle get parameters when they are some.
In my views
#if($search !== null)
{{ $posts->appends($search)->links() }}
#else
{{ $posts->links() }}
#endif
Now replace
{{ $posts->links() }}
The solution was $var->**withPath()** and handling GET parameters

Laravel if request not empty add orWhere

public function index(Request $request) {
if ($request->has('deleted')) {
$assistants = Assistant::onlyTrashed()->where(1);
if ($request->has('firstName'))
$assistants = $assistants->orWhere('firstname', 'LIKE', $request->firstName.'%');
if ($request->has('lastName'))
$assistants = $assistants->orWhere('lastname', 'LIKE', $request->lastName.'%');
if ($request->has('email'))
$assistants = $assistants->orWhere('email', 'LIKE', $request->email.'%');
} else {
$assistants = Assistant::all()->where(1);
if ($request->has('firstName'))
$assistants = $assistants->orWhere('firstname', 'LIKE', $request->firstName.'%');
if ($request->has('lastName'))
$assistants = $assistants->orWhere('lastname', 'LIKE', $request->lastName.'%');
if ($request->has('email'))
$assistants = $assistants->orWhere('email', 'LIKE', $request->email.'%');
}
return $this->showAll($assistants);
}
I am trying to check if firstName, lastName or email is not empty, add to query with LIKE command.
But it returns an error :
Type error: Too few arguments to function
Illuminate\Support\Collection::where(), 1 passed
in Laravel 5.6.
You have multiple problems.
where(1) is not a valid Query Builder call. You also don't seem to need this.
You don't need to repeat all of these request->has() calls, put them below the if ... else ...
Assistants::all() will actually run a query and return all rows in a collection. Use Assistants::query() to return a Query Builder instance.

Laravel 5.6 search forms

In an app I'm working on there are two main searches: a site wide search and a quick search.
Quick search
The quick search has to take the role, user and department and return all the users that match the criteria. I already see a potential problem in the fact that you could select a role that isn't in a department... but anyway.
This is what I've tried.
public function userSearch(Request $request)
{
$department = $request->get('department');
$role = $request->get('role');
$location = $request->get('location');
$users = User::where('department', $department)
->where('role', $role)
->where('location', $location)
->get();
foreach($users as $user)
{
echo '<br />' . $user->username;
}
}
It gets complicated though as user, role and department are drop down boxes and may not have had anything selected.
So If I search for just Digital as a department the query string is:
http://127.0.0.1:8000/usersearch?department=Digital&role=&location=
Obviously, this doesn't return anything as I'm using multiple where clauses.
Would I have to individually check whether each variable is empty and construct the query? This seems a bit sketchy as I'd have to check every possible order to get the query right.
Site wide search
The other search is a site search that takes one query string, here is my stupidly basic method startings:
public function search(Request $request)
{
$search = $request->get('q');
return view('pages.search.index', compact('search'));
}
I read in the Laravel documentation that there is a package available called Scout but is there a more rudimentary way to just get everything from every model, given the query string?
Things I'd like to return:
Users
Teams
Documents with similar titles
News articles
I have models associated to database tables for all the above so could I just do the following?
users = User::where('name', 'like', '%' . Input::get('name') . '%')
->orWhere('name', 'like', '%' . Input::get('name') . '%')
articles= Article::where('name', 'like', '%' . Input::get('name') . '%')
->orWhere('name', 'like', '%' . Input::get('name') . '%')
and so on...
Then when displaying results just do:
return('nameofview', compact('users', 'articles')
Or is this likely to be slow and cumbersome?
Update
For the site-wide search, for now, I just have:
public function search(Request $request)
{
$search = $request->get('q');
$users = User::where('username', 'like', '%' . $request->get('q') . '%')
->orWhere('displayName', 'like', '%' . $request->get('q') . '%')
->orWhere('email', 'like', '%' . $request->get('q') . '%')
->orWhere('role', 'like', '%' . $request->get('q') . '%')
->orWhere('department', 'like', '%' . $request->get('q') . '%')
->orWhere('location', 'like', '%' . $request->get('q') . '%')
->orWhere('directDialIn', 'like', '%' . $request->get('q') . '%')
->orWhere('mobileNumber', 'like', '%' . $request->get('q') . '%')->get();
return view('pages.search.index', compact('search', 'users'));
}
For the quick search, I added some query scopes
/**
* Scope a query by department
*/
public function scopeByDepartment($query, $department)
{
return $query->where('department', $department);
}
/**
* Scope a query by role
*/
public function scopeByRole($query, $role)
{
return $query->where('role', $role);
}
/**
* Scope a query by location
*/
public function scopeByLocation($query, $location)
{
return $query->where('location', $location);
}
There is a much better way to achieve what you are looking to do using a combination of conditional clauses and scopes. For example...
User::all()->when(isset($request->department), function ($q) use ($request) {
$q->byDepartment($request->department);
})
->when(isset($request->role), function($q) use ($request) {
$q->byRole($request->role);
});
Once you set up your scopes on the model you can now selectively filter by any number of the request variables so you don't have to worry if it is not set. This also keeps your code much more compact.

How to combine multiple methods without repeating queries in Controller?

I have a method index
protected function index(Request $request)
{
$articles = Article::published()->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation(),
$this->getCategory($request)
]);
}
And a method getCategory()
public function getCategory($request)
{
if ($request->has('category')){
$search = $request->get('category');
$articles = Article::published()
->where('orientation', 'LIKE', '%' . $search . '%')
->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation()
]);
}
}
As you can see, I try to get my getCategory function outside of my index function.
It works, only, with my debug bar, I have all the SQL queries (those of index and those of getCategory).
Is it possible to optimize my code? Can you help me ?
Thank you
If you need both results add cache for query in index and get full result from cache.
Or if you have all results maybe use collections filter for same your collection from index and provide display data what you need.
For example getCategory add param where you set your collection from index. In article add filter to get only data what you are interest in.
I'm nowhere near an editor to chekc this, but here goes. First function:
public function index(Request $request)
{
$articles = Article::published()->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation(),
//$this->getCategory($request) - this i don't think can work like this. You are calling
//a function that does not return a value, but instead loads a view, insted:
'categories' => $this->getCategory($request)//the idea here is you call a function and
//get a return as an array
]);
}
Second function:
public function getCategory($request)
{
if ($request->has('category')){
$search = $request->get('category');
$articles = Article::published()
->where('orientation', 'LIKE', '%' . $search . '%')
->paginate(8);
$result = [
'articles' => $articles,
'orientation' => $this->getOrientation()
];
}
else $result = null;
return $result;
}
This might work, once again, im a noob. But i don't see why you need 2 methods for this since there is a if ($request->has('category')){ which will yield a null. Idk, give it a try...

Categories