我正在使用Laravel 5.2的控制器,并试图迭代一个有说服力的invoice_items集合,这将转换为订单项目。因此,发票将作为订单,具有订购商品(invoice_item),invoice_items将列出所有订购的产品(产品)。
以下是我所拥有的:
$id = $value; //from param
$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::all()->where('invoice_id', $invoice[0]->id);
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
foreach($invoice_items as $item) {
$products = Product::all()->where('id', $item->product_id);
}
我试图从特定发票中提取所有产品(通过发票项目),在这种情况下应该是两种不同的产品。
发生了什么,当我迭代使用该循环时,它会将相同的产品添加两次,而应该添加每个产品一次。我的逻辑在这里错了吗?或者我是否需要再次查看我的关系?
答案 0 :(得分:1)
将您的查询更改为:
Rufus version: 2.2.668
Windows version: Windows 10 64-bit
Syslinux versions: 4.07/2013-07-25, 6.03/2014-10-06
Grub versions: 0.4.6a, 2.02~beta2
Locale ID: 0x0409
Found USB 2.0 device 'Generic- Multi-Card USB Device' (0BDA:0158)
1 device found
Disk type: Removable, Sector Size: 512 bytes
Cylinders: 979, TracksPerCylinder: 255, SectorsPerTrack: 63
Partition type: MBR, NB Partitions: 1
Disk ID: 0x00000000
Drive has a Zeroed Master Boot Record
Partition 1:
Type: FAT32 (0x0b)
Size: 7.5 GB (8048869376 bytes)
Start Sector: 8192, Boot: No, Recognized: Yes
Scanning image...
'G:\2017-01-11-raspbian-jessie.img' doesn't look like an ISO image
Image has a Zeroed Master Boot Record
'G:\2017-01-11-raspbian-jessie.img' is a bootable disk image
Using image: 2017-01-11-raspbian-jessie.img
Format operation started
Requesting disk access...
Opened drive \\.\PHYSICALDRIVE1 for write access
Will use 'F:' as volume mountpoint
I/O boundary checks disabled
Analyzing existing boot records...
Drive has a Zeroed Master Boot Record
Volume has an unknown Partition Boot Record
Deleting partitions...
Clearing MBR/PBR/GPT structures...
Erasing 128 sectors
Writing Image...
write error: [0x00000002] The system cannot find the file specified.
RETRYING...
write error: [0x00000002] The system cannot find the file specified.
RETRYING...
write error: [0x00000037] The specified network resource or device is no longer available.
0 devices found
Found USB 2.0 device 'Generic- Multi-Card USB Device' (0BDA:0158)
1 device found
No volume information for drive 0x81
Disk type: Removable, Sector Size: 512 bytes
Cylinders: 979, TracksPerCylinder: 255, SectorsPerTrack: 63
Partition type: MBR, NB Partitions: 1
Disk ID: 0x00000001
Drive does not have an x86 Master Boot Record
Partition 1:
Type: Small FAT16 (0x04)
Size: 7.5 GB (8053063680 bytes)
Start Sector: 0, Boot: No, Recognized: Yes
Found USB 2.0 device 'Generic- Multi-Card USB Device' (0BDA:0158)
1 device found
No volume information for drive 0x81
Disk type: Removable, Sector Size: 512 bytes
Cylinders: 979, TracksPerCylinder: 255, SectorsPerTrack: 63
Partition type: MBR, NB Partitions: 1
Disk ID: 0x00000001
Drive does not have an x86 Master Boot Record
Partition 1:
Type: Small FAT16 (0x04)
Size: 7.5 GB (8053063680 bytes)
Start Sector: 0, Boot: No, Recognized: Yes
Found USB 2.0 device 'Generic- Multi-Card USB Device' (0BDA:0158)
1 device found
Disk type: Removable, Sector Size: 512 bytes
Cylinders: 979, TracksPerCylinder: 255, SectorsPerTrack: 63
Partition type: MBR, NB Partitions: 2
Disk ID: 0x623FDBF4
Drive has a Zeroed Master Boot Record
Partition 1:
Type: FAT32 LBA (0x0c)
Size: 63 MB (66060288 bytes)
Start Sector: 8192, Boot: No, Recognized: Yes
Partition 2:
Type: GNU/Linux (0x83)
Size: 4 GB (4301258752 bytes)
Start Sector: 137216, Boot: No, Recognized: Yes
Found USB 2.0 device 'Generic- Multi-Card USB Device' (0BDA:0158)
1 device found
Disk type: Removable, Sector Size: 512 bytes
Cylinders: 979, TracksPerCylinder: 255, SectorsPerTrack: 63
Partition type: MBR, NB Partitions: 2
Disk ID: 0x623FDBF4
Drive has a Zeroed Master Boot Record
Partition 1:
Type: FAT32 LBA (0x0c)
Size: 63 MB (66060288 bytes)
Start Sector: 8192, Boot: No, Recognized: Yes
Partition 2:
Type: GNU/Linux (0x83)
Size: 4 GB (4301258752 bytes)
Start Sector: 137216, Boot: No, Recognized: Yes
更简单的方法是将项目关系添加到InvoiceItems模型。 E.g:
$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::where('invoice_id', $invoice[0]->id)->get();
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
foreach($invoice_items as $item) {
$products = Product::where('id', $item->product_id)->get();
}
然后您可以使用以下方式从Invoice_item获取所有项目:
public function items()
{
$this->hasOne('Items');
}
您也可以尝试:
return $invoice_items->items;
希望$invoice = Invoice::where('id', $id)->get();
$invoice_items = Invoice_item::where('invoice_id', $invoice[0]->id)->get()->lists('product_id');
$contact = Contact::where('id', $invoice[0]->contact_id)->get();
$products = Product::whereIn('id', $invoice_items)->get();
将包含该发票的产品集合。不需要foreach循环。