zahidnazirkhan's avatar

Laravel relationship methods

Hello Everybody,

I had implemented user audit in one of my applications with laravel, but using DB facade since I had not much expertise in ORM relationship magic methods.

Now I want to implement the same using ORM relationship methods like belongsTo() in my audit model. But I am facing issues for which I want to put some points below:

  1. I have to implement Server Side Pagination.
  2. I have to get limited number of records from backend w.r.t Server Side Pagination.
  3. I have to use offset() & limit() methods based upon the requirement.
  4. Maybe I am not having enough expertise with these so called magic methods.

The code for my Audit model is like below:

public function users() {
    return $this->belongsTo('App\Models\User','user_id');
}

The code in my Controller is like this (I want to update it using ORM relationship methods):

$totalData = \DB::table('audit')
            ->join('users', 'users.id', '=', 'audit.user_id')
            ->select('audit.*', 'users.*')
            ->whereIn('users.id',$users)
            ->get()->count();
$auditDetails = \DB::table('audit')
                ->join('users', 'users.id', '=', 'audit.user_id')
                ->select('audit.*', 'users.name')
                ->whereIn('users.id',$users)
                ->offset($start)
                ->limit($limit)
                ->orderBy($order,$dir)
                ->get();

In addition to above code, there is more code related to datatables, that's not a concern for me thus far.

So my query is how can I update the above code in Controller so that I can use ORM relationship methods with ease in future implementations.

Waiting to hear from you...!!!

Thankyou

0 likes
11 replies
Snapey's avatar

Please can you edit your question and format the code blocks with ``` on a line before and after each

Snapey's avatar

something like


$auditDetails = Audit::with('users')->paginate(20);

$totalData = $auditDetails->count();

Although this last line, I would just do in the view.

zahidnazirkhan's avatar

Dear Sir,

Updated as you directed.

FYI I am not using Laravel paginator. I am using Javascript DataTables and fetching records via an AJAX call.

Waiting to hear from you...!!!

Snapey's avatar

You can still use the Laravel paginator, just append ?page=n to your ajax call to get the next block of records.

My previous response not using paginator;

$auditDetails = Audit::with('users')->get();

$totalData = $auditDetails->count();
zahidnazirkhan's avatar

I will give that a try indeed.

If we speak of paginator, shall I correlate it to the fact that I can achieve Server Side Pagination while using paginator alone without using any DataTable like feature/tool.

Snapey's avatar

I don't know what that means...

If you say what type of request your ajax call is making, we can advise the right controller method to return paginated data.

if you make a call to /audits?page=3 then when the ->paginate(20) function executes it looks to see if 'page' is specified in the request and then creates limit and offset values for the query

zahidnazirkhan's avatar

Dear Sir,

I am achieving Server Side Pagination using javascript DataTables library. I can customize things as per requirement using datatables.

Preparing data, setting the limit & offset values in the controller at each call is being managed.

I simply wanted to know what is the best standard practice for Server Side Pagination. Whether to rely on Paginator (either Simple or Lengthaware) or using Javascript DataTables?

Currently I implement these things using Javascript DataTables library that takes care of my all needs & from the back end Controller prepared data is sent to the view via an AJAX call. I do manage length, limit & offset values there as well.

If you can illuminate me more on this concept, I would highly appreciate that.

Snapey's avatar

So what API do you have for your javascript pagination?

What parameters does it pass to the server to request each page?

The code in your original question does not do any pagination?

zahidnazirkhan's avatar

Yes indeed sir, I mentioned in my original question that there is more code related to datatables.

The code that I shared was meant to be optimized for ORM relationship methods.

The pagination thing is internally managed by Javascript DataTables API. At every call per page, required arguments like length, page, limit & offset values are sent to the controller in addition to the actual data. I can manage a lot using Javascript DataTables API, even I can control which fields to search for on a DataTable while implementing Server Side Pagination.

Your reply to my question involved paginator, thats why I asked some information in that regard as I am not using Paginator.

Since in my case, I am using AJAX I am actually avoiding page reloads.

Snapey's avatar

Then I dont know what else you want to know

zahidnazirkhan's avatar

Dear Sir Greetings for the day,

I simply wanted to know that if I want to implement Server Side pagination, is Laravel Paginator enough alone?

In response to your answers w.r.t my question, I have optimized my entire code to be used by ORM relationship methods.

Please or to participate in this conversation.