therons's avatar

pagination page # for specific record by ID?

Hi Everyone, After inserting a company a record. I would like to retrieve the paginated page where the newly created is sitting. After the record is successfully inserted I redirect to a view that has a paginated list of companies that are sorted in alphabetical order.

So basically I'd like to look within the pagination collection for the company id of x and have it return the pagination page # that the where the record resides in the sorted list.

Any ideas?

    public function show($id)
    {
        $companies = Company::companyNameAscending()->paginate(300);

        // need the (paginated) page number that $id is going to be sitting on within $companies 
        
        $company = Company::findOrFail($id);
        $notes = $company->notes()->dateCreatedDescending()->get();
        $company_address = $company->companyAddress()->get();
        $contacts = $company->contacts()->limit(100)->get();

        return view('companies.show', compact('companies', 'company', 'notes', 'company_address', 'contacts'));

    }
0 likes
10 replies
jlrdw's avatar

Normally done in an edit mode, you return where you left off, not normally done in anew add mode.

therons's avatar

Totally, but for the system after a record is added I go to the newly created record. Which also has the paginated list of companies so a user can hop over to another record without leaving the page.

jlrdw's avatar

How could that be possible when the pagination calculations has to take place again and that record be properly placed in the correct position? Sorry just confused on what you are driving at.

therons's avatar

Basically after a new record company is inserted I'd like that list of companies to automatically load the paginated page where the newly inserted company resides. So let's say you have total 6 companies. Pagination is set to 2 records / page. The Company list is sorted Alphabetically.

Pre company paginated page list

Page 1
   Company 1
   Company 2
Page 2
   Company 3
   Company 4
Page 3
   Company 5
   Company 6 

Post company paginated page list

Page 1
   Company 1
   Company 2
Page 2
   Company 3
   Company 3 new insert // show this item  after insert is completed on the correct paginated page
Page 3
   Company 4
   Company 5
Page 4
   Company 6 

so i need to somehow figure out that Company 3 new insert resides on paginated page=3

veve286's avatar
veve286
Best Answer
Level 8

Sample code.


$position=Company::where('id','<=',$inserted_id)->count(); // for example 601
$inserted_id_in_page=$position/300; // then goes to page 2

therons's avatar

@veve286 that's a great assertion and it almost worked except the fact that I'm needing it be ordered by "company->name".

        $position = Company::where('name', '<=', $company->name)->orderBy('name', 'DESC')->count();

but as soon as I add the where and the orderby it returns the records by order of ID not name? So the count is incorrect

Also, I'm using Postgresql. Any other ideas?

schir1964's avatar

You could create a method on the Company model that would return the proper paginated page number.

This method would create the sorted list containing just the id column.

Transform sorted list to a simple array.

Determine the index (position) of the item in the array (not by the id).

Then the method could determine which page the id would be shown and return that page number.

This routine would add a lot of overhead your code if it has to be called much.

therons's avatar

Thanks for the reply @schir1964 I've made a quick test o what you were saying to do. It ends up taking a full second to generate the data from the Query which of course is to much. Any other ideas?

therons's avatar
 $companies = Company::with([
            'companyaddress' => function ($query) {
                $query->orderBy('country', 'desc');
            }
        ])->companyNameAscending()->paginate(300);
        echo "<pre>";
        var_dump($companies->all());

So in pushing this thought further i noticed something if i can figure out how to access it that will do the trick.

When doing a dump of a collection the item number is appended to the object name specifically #618 . Anyone know how to get access to that number? It does not seem to be stored in the object it's self an I haven't found the methods that help in the naming of the object in the collection.

Company {#618 ▼
  #table: "companies"
  #primaryKey: "companyid"
  #fillable: array:23 [▶]
  #connection: null
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:23 [▶]
  #original: array:23 [▶]
  #relations: array:1 [▶]
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}
therons's avatar

This is a dump of the whole object for context. So, as of right now I'm trying to figure out what the #'s #314 and #618 represent and how I can access them. Noticing the numbers are next to objects only and not arrays. Any ideas?

object(ArrayIterator)#314 (1) {
  ["storage":"ArrayIterator":private]=>
  array(300) {
    [0]=>
    object(App\Company)#618 (23) {
      ["table":protected]=>
      string(9) "companies"
      ["primaryKey":protected]=>
      string(9) "companyid"
      ["fillable":protected]=>
      array(23) {
        [0]=>
        string(9) "companyid"
        [1]=>
        string(4) "name"
        [2]=>
        string(7) "website"
        [3]=>
        string(5) "email"
        [4]=>
        string(8) "division"
        [5]=>
        string(8) "refersto"
        [6]=>
        string(14) "aimscustnumber"
        [7]=>
        string(11) "creditlimit"
        [8]=>
        string(13) "creditgrantdt"
        [9]=>
        string(5) "taxid"
        [10]=>
        string(14) "courieraccount"
        [11]=>
        string(9) "updatedby"
        [12]=>
        string(9) "updatedon"
        [13]=>
        string(8) "endeffdt"
        [14]=>
        string(12) "maxcompanyid"
        [15]=>
        string(15) "accesscompanyid"
        [16]=>
        string(16) "caaimscustnumber"
        [17]=>
        string(14) "verificationdt"
        [18]=>
        string(12) "currencycode"
        [19]=>
        string(11) "flagimpound"
        [20]=>
        string(17) "rsaaimscustnumber"
        [21]=>
        string(16) "mergetocompanyid"
        [22]=>
        string(12) "previousname"
      }
      ["connection":protected]=>
      NULL
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["timestamps"]=>
      bool(true)
      ...

Please or to participate in this conversation.