php - Laravel Eloquent - how to relate table to another by two fields -


having 2 tables user , mail

mailtable:

\mail \--|id \--|user_id \--|from_user_id \--|other_fields 

usertable:

\user \--|id \--|name \--|other_fields 

the mail table related user 2 fields => user_id , from_user_id, how use eloquent fetch data related user inbox , outbox(sent) 1 user?

not sure how models have been defined, should work.

namespace app;  use illuminate\database\eloquent\model;  class mail extends model {     public function user()     {         return $this->hasone('app\user');     }      public function from_user()     {         return $this->hasone('app\user', 'id', 'from_user_id');     } } 

then fetch, use: $from_user = $mail->from_user

adjust namespaces according how they've been defined in application.


Comments

Popular posts from this blog

Combining PHP Registration and Login into one class with multiple functions in one PHP file -

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -