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
Post a Comment