Reputation: 639
Having two tables User
and Mail
MailTable
:
\mail
\--|id
\--|user_id
\--|from_user_id
\--|other_fields
UserTable
:
\user
\--|id
\--|name
\--|other_fields
the Mail
table is related to user by two fields => user_id
and from_user_id
, how to use eloquent to fetch data related to user to get inbox and outbox(sent) for one user?
Upvotes: 0
Views: 843
Reputation: 25935
Not sure how your models have been defined, but something like this 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 to fetch, use: $from_user = $mail->from_user
Adjust the namespaces according to how they've been defined in your application.
Upvotes: 2