我想在 Laravel 中创建一个功能
我想在 Laravel 上创建一个功能来检查数据库表的行是否存在。检查后,根据检查结果,应删除、添加新列或跳过它们。
foreach($this->category_product as $k => $item) {
$category_product = CategoryProduct::where([['category_id', $this->category_id],['product_id', $k]])->firstOr(function () {
$category_product = CategoryProduct::create([
'category_id' => $this->category_id,
'product_id' => $k
]);
});
}
这是我到目前为止所拥有的。如何删除查询中不存在的行? 任何帮助将不胜感激。 谢谢
I want to create a functionality on Laravel that would check the rows of a database table for existence. After checking, depending on the results of the check, it should delete, add new columns or skip them.
foreach($this->category_product as $k => $item) {
$category_product = CategoryProduct::where([['category_id', $this->category_id],['product_id', $k]])->firstOr(function () {
$category_product = CategoryProduct::create([
'category_id' => $this->category_id,
'product_id' => $k
]);
});
}
Here's what I have so far. How to remove rows that do not exist in a query?
Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望我理解正确,因为问题不够清楚,但这里有一种删除行(如果存在)的方法:
注意:您缺少
use ($k)
I hope I understood you right, as the question isn't clear enough, but here is a way to delete the rows if they exist:
Note: you were missing
use ($k)