如果之前已经回答过这个问题,我深表歉意,但是我似乎没有发现类似的情况。如果它是重复的,请指出我正确的方向。
我有三个自定义表:
> app_expressions;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| expression | varchar(191) | NO | | NULL | |
| bot | int(10) unsigned | NO | MUL | NULL | |
+------------+------------------+------+-----+---------+----------------+
> app_links;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| source | varchar(10) | NO | MUL | NULL | |
| destination | varchar(10) | NO | MUL | NULL | |
| sid | int(10) unsigned | NO | MUL | NULL | |
| did | int(10) unsigned | NO | MUL | NULL | |
| bot | int(10) unsigned | NO | MUL | NULL | |
+-------------+------------------+------+-----+---------+----------------+
> app_replies;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| reply | text | NO | | NULL | |
| bot | int(10) unsigned | NO | MUL | NULL | |
+-------+------------------+------+-----+---------+----------------+
app_expressions
连接到app_replies
通过app_links
假设我在每个表中都有一条记录。
app_expressions.id
和app_links.sid
where一样app_links.source = 'expression'
app_replies.id
和app_links.did
where一样app_links.destination = 'reply'
使用 Laravel 的hasOneThrough()
我如何通过条件访问app_expressions
's 回复?伪代码如下:app_replies
app_links
app_links.destination = 'reply'
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Expressions extends Model
{
/*
* Table
*/
protected $table = 'app_expressions';
/*
* Get all replies linked with this record
*/
public function get_reply()
{
return $this -> hasOneThrough(
'App\Replies',
'App\Links',
'did', // Destination ID
'id',
'id',
'sid' // Source ID
) -> where('intermediary table.destination', '=', 'reply') ;
}
}
我希望我解释得很好。