【发布时间】:2020-10-24 18:54:16
【问题描述】:
我有一个具有以下关系的 WorkPackage 模型。
public function work_items()
{
return $this->hasMany(WorkItem::class);
}
WorkPackage 有 'title'、'project_id' 列,work_items 有 'title'、'work_package_id' 列。现在我想搜索用户键入的关键字与两个表中的标题匹配。
这是我尝试过的功能
public function getWorkPackageProposals($projectId, $title)
{
$result['data'] = $this->workPackage->with(['work_items' => function ($query) {
$query->where('title', 'LIKE', "%{$title}%");
}])
->where('project_id', $projectId)
->where('title', 'LIKE', "%{$title}%")
->get();
return $result;
}
但它不起作用。 请在下面找到我用来创建 WorkPackage 对象的代码。
public function __construct(WorkPackage $workPackage)
{
$this->workPackage = $workPackage;
$this->setModel(WorkPackage::class);
$this->workItemRepository = app(WorkItemRepository::class);
}
【问题讨论】:
-
你能解释一下你想在这里实现什么吗?
-
我有两个具有上述关系的表,WorkPackage 表有 'title'、'project_id' 和 work_items 表有 'title'、'work_package_id' 列。现在我想用两个表的标题列匹配的两个表搜索关键字。
标签: laravel relationship eloquent-relationship