使用eloquent orm的SQL有等同的减号吗?

例如

$ model1= model :: where('应用了一些约束') $ model2= model ::其中('某些约束应用')

我想获得$ model1中存在的所有型号,但不是$ model2

有帮助吗?

解决方案

secblaze的答案看起来很好,但它将运行3个查询。另一个选项是集合对象的diff()方法:

$result = $model1->diff($model2);
.

此工作在从DB中获取数据后,使用2个查询,而是完整的数据集(除非有更多根据您的“约束”)。

其他提示

我看到的最简单方法是:

//Get the id's of first model as array
$ids1 = $model1->lists('id');

//get the id's of second models as array
$ids2 = $model2->lists('id');

//get the models
$models = Model::whereIn('id',$ids1)->whereNotIn('id',$ids2)->get();
.

这不是测试代码,请阅读更多关于eloquent queries

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top