I am trying to use paginate
while selecting data from database by using relationships. I have three tables namely, users
, contacts
and contact_messages
with models User
, Contact
, ContactMessage
respectively.
I am trying to fetch all messages of a particular user by using the following method :
public function listMessage($user_id){
$user1 = User::find($user_id);
$messages = $user1->contact_messages;
return View::make('message.listmessage')
->with('user_id', $user_id)
->with('messages', $messages);
}
By this method I am getting all messages, but I couldn't paginate $messages
. How can I paginate $messages
? Can anyone help?
My models and relationships are given below :
class User extends Authenticatable
{
public function contacts()
{
return $this->hasMany('App\Contact');
}
public function contact_messages()
{
return $this->hasManyThrough('App\ContactMessage', 'App\Contact');
}
}
and Contact Model is
class Contact extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function messages()
{
return $this->hasMany('App\ContactMessage');
}
}
finally ContactMessage model is
class ContactMessage extends Model
{
public function contact(){
return $this->belongsTo('App\Contact');
}
}