0

如果之前已经回答过这个问题,我深表歉意,但是我似乎没有发现类似的情况。如果它是重复的,请指出我正确的方向。

我有三个自定义表:

> 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.idapp_links.sidwhere一样app_links.source = 'expression'
  • app_replies.idapp_links.didwhere一样app_links.destination = 'reply'

使用 Laravel 的hasOneThrough()我如何通过条件访问app_expressions's 回复?伪代码如下:app_repliesapp_linksapp_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') ;
    }
}

我希望我解释得很好。

4

2 回答 2

1

解决了!我不得不使用数据透视表特定的方法。这是供参考的代码。

<?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 -> belongsToMany(
                            'App\Replies', // The destination table (app_replies)
                            'app_links',  // The pivot table; It can take a Model as argument as well (app_links)
                            'sid',        // Foreign key on pivot table (app_expressions.id on app_links.sid)
                            'did',        // Wanted key on pivot table (app_replies.id on app_links.did)
                            'id',         // Foreign key (app_expressions.id)
                            'id'          // Wanted key (app_replies.id)
                        )         

                        -> wherePivot('destination', 'reply'); // app_links.destination = 'reply'

    }

理念:我正在从app_replies、 through app_links、 using app_expressions、 ifapp_links.sid = app_expressions.id和访问记录app_links.destination = 'reply'。修补程序的测试代码如下:

$app = new App\Expressions;

$expression = $app -> first();

$reply = $expression -> get_reply;

有关更多信息,我建议寻找声明的方法的参数\Illuminate\Database\Eloquent\Relations\BelongsToMany\HasRelationship

于 2019-10-15T17:47:35.650 回答
0

在查询时的代码中,您可以使用WhereHas根据通过表过滤器过滤您的表,例如:

$str='reply';
$data=Expressions::whereHas('get_reply',function($q) use($str){
$q->where('app_links.destination',$str);
})->get();

并且不要忘记删除表达式模型上的这一行:

-> where('intermediary table.destination', '=', 'reply') ;
于 2019-10-15T16:11:57.803 回答