在条件下从集合中删除特定对象

时间:2017-02-08 13:24:46

标签: laravel

我有一组对象,它们是DB中的行。

enter image description here

在我的控制器中,我拨打所有fileuploads和所有userfileuploads

$allFileuploads = Fileupload::all();
$existingFileuploads = UserFileupload::where('userid', $user->id)->get();

Fileupload模型和UserFileupload模型没有相同的属性。

如何从$allFileuploads集合id = fileupload_id收集来自$existingFileuploads集合的文件上传文件?

更新

我现在尝试了这个,我得到一个空集合:

$allFileuploads = Fileupload::get();
$existingFileuploads = UserFileupload::where('userid', $user->id)->get();
$existingIds = $existingFileuploads->pluck('id')->toArray();

$selectedFiles = $allFileuploads->filter(function ($file) use ($existingIds) {
    return in_array($file->id, $existingIds);
});

dd($selectedFiles);

更新

它适用于已接受的答案...我使用了错误的名称来获取fileupload_id

1 个答案:

答案 0 :(得分:2)

尝试使用此approch。

$allFileuploads = Fileupload::get();
$existingFileuploads = UserFileupload::where('userid', $user->id)->get();
$existingIds = $existingFileuploads->pluck('id')->toArray();
$selectedFiles = $allFileuploads->filter(function ($file) use ($existingIds) {
    return in_array($file->id, $existingIds);
});

首先来自existingFileuploads的{​​{3}} ids,然后$allFileuploads

中的plucks来自select @xml --, @XML.query('Document/Content/Truncated') ff -- URL -- , @xml.value('(Document/Metadata/Fields/docid)[1]', 'varchar(100)') D_URL -- title -- , @xml.value('(Document/Metadata/Fields/title)[1]', 'varchar(100)') Title -- name -- , @xml.value('(Document/Metadata/Facets/Facet/_ne/person)[1]', 'varchar(100)') MD_Name -- address -- , left((substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Location & Contact%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 550)), CHARINDEX ('[MAP]' , (substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Location & Contact%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 550)) ))Office_Location -- gender-- , substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Years in Practice%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 10) Gender -- phone -- ,right(left((substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Location & Contact%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 550)), CHARINDEX ('Phone Number' , (substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Location & Contact%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 550)) )), CHARINDEX ('(' , (substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Location & Contact%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 550)) ))Phone -- speciality -- , substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Specialties & Qualifications%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 100) Specialities -- type-- , substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Doctor Type%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 15) Doctor_Type -- Explenation -- , substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Specialty:%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 400) Explenation -- certification -- , substring(@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') , 18+patindex('%Certifications & Licensure%', (@xml.value('(Document/Content/Text)[1]', 'nvarchar(4000)') )), 200) Board_Certifications