Lumen Eloquent 关系
Lumen Eloquent Relations
更新
我的方法是接收等同于 simpel mysql 连接的东西。我想要来自 tabel tx 的所有条目 project_id = x 与 tx.id = txevent.tx_id.
上的 txevent 连接
我使用流明 5.5
更新 2
下面的原始 sql 将完全满足我的要求:
$rawTx = DB::>table('tx')
->join('txevent', 'txevent.tx_id', '=', 'tx.id')
->where('tx.project_id', $request->get('project_id'))
->where('txevent.short_id', $request->get('short_id'))
->get();
有没有可能通过关系达到同样的效果?
更新结束
我有 2 个表 tx
和 txevent
:
发送:
id, int, autoincrement
project_id, int
txevent:
id, int, autoincrement
tx_id, int, fk(tx.id)
shortnumber, char
在tx.php中我有以下方法:
public function txeventWithShortnumber($shortnumber)
{
return $this->hasOne('App\Txevent')->where('shortnumber', $shortnumber)->first();
}
在TxController.php我会做:
$tx = Tx::where('project_id', $request->get('project_id'))->txeventWithShortnumber($request->get('shortnumber'))->get();
结果我收到以下错误消息:
(1/1) BadMethodCallException
调用未定义的方法 Illuminate\Database\Query\Builder::txeventWithShortnumber()
在 Builder.php 行 2483
谁能告诉我我做错了什么?
我建议这样做:
在Tx.php
public function txEvents()
{
return $this->hasMany(TxEvent::class);
}
public function txeventWithShortnumber($shortNumber)
{
return $this->txEvents->first(function($txevent) use ($shortNumber) {
return $txevent->shortnumber == $shortNumber;
});
}
在控制器中:
$tx = Tx::where('project_id', $request->get('project_id'))->get();
// attach shortNumber event
$tx->map(function($t) use ($request) {
$t->eventWithShortNumber = $t->txeventWithShortnumber($request->get('shortnumber'));
});
$tx
变量现在也将保存具有给定短编号的 txEvents。
我不确定您是否可以将条件动态传递给 eloquent 关系。
更新
我的方法是接收等同于 simpel mysql 连接的东西。我想要来自 tabel tx 的所有条目 project_id = x 与 tx.id = txevent.tx_id.
上的 txevent 连接我使用流明 5.5
更新 2
下面的原始 sql 将完全满足我的要求:
$rawTx = DB::>table('tx')
->join('txevent', 'txevent.tx_id', '=', 'tx.id')
->where('tx.project_id', $request->get('project_id'))
->where('txevent.short_id', $request->get('short_id'))
->get();
有没有可能通过关系达到同样的效果?
更新结束
我有 2 个表 tx
和 txevent
:
发送:
id, int, autoincrement
project_id, int
txevent:
id, int, autoincrement
tx_id, int, fk(tx.id)
shortnumber, char
在tx.php中我有以下方法:
public function txeventWithShortnumber($shortnumber)
{
return $this->hasOne('App\Txevent')->where('shortnumber', $shortnumber)->first();
}
在TxController.php我会做:
$tx = Tx::where('project_id', $request->get('project_id'))->txeventWithShortnumber($request->get('shortnumber'))->get();
结果我收到以下错误消息:
(1/1) BadMethodCallException 调用未定义的方法 Illuminate\Database\Query\Builder::txeventWithShortnumber()
在 Builder.php 行 2483
谁能告诉我我做错了什么?
我建议这样做:
在Tx.php
public function txEvents()
{
return $this->hasMany(TxEvent::class);
}
public function txeventWithShortnumber($shortNumber)
{
return $this->txEvents->first(function($txevent) use ($shortNumber) {
return $txevent->shortnumber == $shortNumber;
});
}
在控制器中:
$tx = Tx::where('project_id', $request->get('project_id'))->get();
// attach shortNumber event
$tx->map(function($t) use ($request) {
$t->eventWithShortNumber = $t->txeventWithShortnumber($request->get('shortnumber'));
});
$tx
变量现在也将保存具有给定短编号的 txEvents。
我不确定您是否可以将条件动态传递给 eloquent 关系。