如果这个问题之前已经回答过,我诚挚的道歉,但是我似乎没有找到类似的情况。如果重复,请指出正确的方向。
我有三个自定义表:
> 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
中的app_links.source = 'expression'
app_replies.id
与app_links.did
中的app_links.destination = 'reply'
如何使用Laravel的hasOneThrough()
在条件app_expressions
下通过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') ;
}
}
的回复?伪代码如下:
userprofile
我希望我解释得很好。
答案 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_replies
和app_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
上声明的方法的参数