Please can you edit your question and format the code blocks with ``` on a line before and after each
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:
- I have to implement Server Side Pagination.
- I have to get limited number of records from backend w.r.t Server Side Pagination.
- I have to use offset() & limit() methods based upon the requirement.
- 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
Please or to participate in this conversation.