Laravel hasOneThrough();中间 table 自定义列值
Laravel hasOneThrough(); intermediary table custom column value
如果之前有人回答过这个问题,我深表歉意,但我似乎没有找到类似的情况。如果是重复的,请指出正确的方向。
我有三个自定义 table:
> 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_links
连接到 app_replies
假设我在每个 table.
中有一条记录
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') ;
}
}
希望我解释的很好
在您查询时的代码中,您可以使用 WhereHas 根据 Through table 过滤器过滤您的 table 例如:
$str='reply';
$data=Expressions::whereHas('get_reply',function($q) use($str){
$q->where('app_links.destination',$str);
})->get();
别忘了在表达式模型上删除这一行:
-> where('intermediary table.destination', '=', 'reply') ;
解决了!我不得不使用 pivot table 特定的方法。下面是参考代码。
<?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
上声明的方法的参数
如果之前有人回答过这个问题,我深表歉意,但我似乎没有找到类似的情况。如果是重复的,请指出正确的方向。
我有三个自定义 table:
> 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_links
app_replies
假设我在每个 table.
中有一条记录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') ;
}
}
希望我解释的很好
在您查询时的代码中,您可以使用 WhereHas 根据 Through table 过滤器过滤您的 table 例如:
$str='reply';
$data=Expressions::whereHas('get_reply',function($q) use($str){
$q->where('app_links.destination',$str);
})->get();
别忘了在表达式模型上删除这一行:
-> where('intermediary table.destination', '=', 'reply') ;
解决了!我不得不使用 pivot table 特定的方法。下面是参考代码。
<?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