Laravel hasOneThrough();中介表格自定义列值

时间:2019-10-15 15:53:13

标签: php laravel eloquent orm has-one-through

如果这个问题之前已经回答过,我诚挚的道歉,但是我似乎没有找到类似的情况。如果重复,请指出正确的方向。

我有三个自定义表:

> 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.sid中的app_links.source = 'expression'
  • app_replies.idapp_links.did中的app_links.destination = 'reply'

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

userprofile

我希望我解释得很好。

2 个答案:

答案 0 :(得分:0)

在查询时在您的代码中可以使用何处来基于“通过表”过滤器来过滤表,例如:

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

并且不要忘记在Expressions模型中删除此行:

-> where('intermediary table.destination', '=', 'reply') ;

答案 1 :(得分:0)

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

<?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_repliesapp_links,我将使用app_expressions通过app_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

有关更多信息,建议您搜索gsutil notification create -t projects/gcpProjectNameHere/topics/gcpTopicNameHere -e OBJECT_FINALIZE -f json -p parentBucketFolder/childBucketFolder/ gs://gcpBucketNameHere 上声明的方法的参数