What if you start it with $donations = Donation::where('deleted',0)->leftJoin....
Ran into issues creating a simple search filter
Hi there Laracasts, this is my first post on this forum.
I am using Laravel 5.4. So currently I am building a donor management system. I have three tables, Users (which are basically admins), Donors (which are the people who donate) and Donations.
The donations table has the following fields: donation_id user_id (the one who created the donation) donor_id (the one who donated) added_at (date) amount type (Cash, Check, PayPal) recurring (Yes or No) entry_source (PayPal, IPN, Website) validated (Yes or No) validated_date validated_user_id deleted (Yes or No)
I need to create a free form search, so you can search for keywords that are existing in multiple columns of multiple tables. I managed to do this, in DonationController:
$donations = Donation::leftJoin('donors', 'donors.id', '=', 'donations.donor_id')->leftJoin('users', 'users.id', '=', 'donations.user_id')->where('added_at', $value)->orWhere('amount', $value)->orWhere('entry_source', 'LIKE', "%$value%")->orWhere('type', 'LIKE', "%$value%")->orWhere('donors.first', 'LIKE', "%$value%")->orWhere('donors.last', 'LIKE', "%$value%")->orWhere('users.name', 'LIKE', "%$value%")->orWhere('donors.email', 'LIKE', "$value%")->orWhere('donors.email2', 'LIKE', "$value%")->where('deleted', '=', '0')->paginate(15);
This is where the problem starts: I need to display only the donations that are not marked as deleted. This query will display them all, both deleted and not. I can try to do this:
$new = $donations->where('deleted', '0')
But then I can't guarantee that it will display 15 results. If I have, for instance, three deleted donations, it will show only 12 in the view.
I tried to use get() method on the first query, and then use the paginate(15) on collection, but that does not work.
Can someone assist me to either 1) Alter my query, so it searches only for the donations that are not marked as deleted, but still keeps the free form search, where I can search for any column or 2) Show me how to create a pagination on already existing collection.
I am thankful in advance
Hi @deansatch , unfortunately your solution did not work, but I managed to solve it by myself.
I have managed to manually create a pagination for a collection, by doing the following:
In the controller, I created a function:
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
It enabled me to call a paginate() method on a collection, this is how you use it:
$collection = App\Model::where('something', 'somethingelse')->get();
$results = $this->paginate($collection, $perPage = 10, $page = null, $options = ['path' => Paginator::resolveCurrentPath()]);
return view('view.name', ['results' => $results->appends(Input::except('page'))]);
The pagination will work just as the one you call with query builder. You can use {{$results->links()}} in your view, and it will work.
Please or to participate in this conversation.