我在尝试查询 Eloquent 关系时遇到问题。
我有 2 张桌子
tbl_deals
- ID
- 交易ID
- 商家 ID
tbl_merchants
- ID
- 商家 ID
- 商家网址文本
我将交易模型定义为
class deal extends Model
{
public function merchant() {
return $this->hasOne('App\merchant', 'merchant_id', 'merchant_id');
}
}
现在,我想查询所有交易基于 where Mercer_url_text = 我的控制器中的变量
这是我正在尝试的
$deals = deal::with('merchant')->get(); //Retrieving all the deals with merchants
如果我返回 $deals,它会给我所有与商家关系的交易。
我如何通过说 where Mercer_url_text = $variable 来限制交易
我在尝试
return $deals->where('merchant_url_text', $merchant_url_text)->get();
但它给了我一个错误说:
“缺少 Illuminate\Support\Collection::get() 的参数 1,调用...”
我试图在 Laravel Docs 中查找用于查询关系的文档。它谈到了这个例子
$user = App\User::find(1);
$user->posts()->where('active', 1)->get();
在这种情况下,它试图获取第一个用户并找到与该用户相关的相应帖子。
在我的情况下,我想从所有交易中过滤,这些交易在我的控制器中具有 Mercer_url_text = 一个特定变量。
关于我如何实现这一目标的任何帮助?
谢谢