Converting foreign key to Eloquent instance

Posted 2 years ago by StormShadow

Hi everyone!

Given this table (called threads):

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| to         | int(10) unsigned | NO   |     | NULL    |                |
| from       | int(10) unsigned | NO   |     | NULL    |                |
| subject    | varchar(255)     | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

The fields to and from are user ids from the users table. How can I set up eloquent relationships or queries so this works:

$thread->to  = the proper Eloquent instance of User instead of just the numerical user id
$thread->from  = the proper Eloquent  instance of User instead of just the numerical user id

Thanks for your help!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.