0

I have a problem and can't figure out how to solve it. I've searched online but still couldn't get a firm answer. What I'm trying to do is to paginate Inquiries table - or the query below. Everything seems to work (at least I'm getting 20 inquiries per page, however I can't figure out how to display the links()?

I'm saving Inquiries in the $thecusts collection and passing it to the view. Tables and relations : Employees - Dealers (manytomany) Dealers-customers(1 to many) Customers-inquiries (1 to many).

So I need to paginate x Inquiries for the employee that has many dealers.

Anyone can help ?

$justch = $me->employeehasdealers()->get(['dealers.dealer_id','dealer','abbreviation']); //list of dealers assigned to auth employee
$justch2=$justch->load(['DealerHasInquiries'=>function($query){
    $query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->paginate(20);
}]);


$thecusts = new Collection();
foreach ($justch2 as $just) {
    $thecusts = $thecusts->merge($just->DealerHasInquiries);
}
MT513
  • 15
  • 3
  • Why don't you go the other way, e.g., get inquiries corresponding to those dealers and paginate those. – apokryfos Feb 04 '18 at 12:14
  • Well, I'm sorry I'm still learning laravel and this question might be stupid but how do I paginate inquries for dealers since between Dealers table and Inquiries table I got customers table? I can get the customers first, but this way the duration is twice bigger – MT513 Feb 04 '18 at 12:32
  • Well it might look something like `Inquiry::whereHas("dealer", function ($q) use ($me) { $q->whereHas("employee", function ($q) use ($me) { $q->id = $me->id; }); })->paginate(20);` or you can check `https://laravel.com/docs/5.5/eloquent-relationships#has-many-through` to be able to get them directly from the user – apokryfos Feb 04 '18 at 12:42

3 Answers3

0

however I can't figure out how to display the links()?

You can do this in your blade:

{{ $justch2->links() }}
Prince Lionel N'zi
  • 2,510
  • 2
  • 12
  • 27
0

The problem is you're not passing back the paginator instance. You can manually create one in your controller based on $thecusts:

$perPage=20;
return view('my-view', ['paginator' => new Paginator($thecusts->toArray(), $perPage)]);

Then you can call the links method on the paginator instance in your view:

{{ $paginator->links() }}

Edit

You may be able to simply all this though in a single query like:

$thecusts = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query){
    $query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer');
}])->paginate(20);

return view('my-view, compact('thecusts'));
  • Yes, this was my initial query, however I changed it so I could solve the issue with pagination. Now you gave me the true answer, I had to pass the paginator instance and I appreciate it a lot! Thank you !!! – MT513 Feb 04 '18 at 13:02
  • Right on, glad it worked for you. Go ahead and click that check mark next to the answer too ;) –  Feb 04 '18 at 13:05
0

The Answer has been posted by btl - (Much appreciate it!)

Here is my code with some additions:

Controller:

$perpage=19;     
 $justch = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query) use ($perpage){
            $query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->simplePaginate($perpage);
        }])->get(['dealers.dealer_id','dealer','abbreviation']);
     foreach ($justch as $just) {
                $thecusts = $thecusts->merge($just->DealerHasInquiries);
                           }

     $paginator = new Paginator($thecusts->toArray(),$perpage, Paginator::resolveCurrentPage(),['path'=>Paginator::resolveCurrentPath()]);
    $inquiries = $thecusts;

    return view('/inquiries', ['paginator'=>$paginator,'inquiries' => $inquiries]);

Blade: {{$paginator->links()}}

MT513
  • 15
  • 3