使用 foreach 将数据放入方法 (addItem)

Put data in the method (addItem) using foreach

我试过的代码,但它显示 1 个值。我想使用 addItem 插入更多数据并将其显示在我的发票 pdf 上。我可以使用 eloquent 获取所有数据,但不知道如何插入给定的 class。

    public function invoice($id)
    {
        $motorcycle_service = MotorcycleService::findOrFail($id);

        foreach ($motorcycle_service->items as $item){

            $invoice = \ConsoleTVs\Invoices\Classes\Invoice::make()
                ->addItem($item->item->name, $item->item->price, $item->quantity, $item->id)
                ->number($motorcycle_service->id)
                ->with_pagination(true)
                ->duplicate_header(true)
                ->date(Carbon::parse($motorcycle_service->created_date))
                ->notes('Expected date and time to complete: ' . Carbon::createFromFormat('Y-m-d H:i:s', $motorcycle_service->expected_date)->format('d/m/Y h:i a'))
                ->customer([
                    'name'      => $motorcycle_service->motorcycle->customer->name,
                    'phone'     => $motorcycle_service->motorcycle->customer->phone_no,
                    'id'     => $motorcycle_service->motorcycle->customer->id,
                    'email' => $motorcycle_service->motorcycle->customer->email
                ])
                ->show($motorcycle_service->id);
        }

        return $invoice;
    }

示例代码位于 https://github.com/ConsoleTVs/Invoices

$invoice = \ConsoleTVs\Invoices\Classes\Invoice::make()
                ->addItem('Test Item', 10.25, 2, 1412)
                ->addItem('Test Item 2', 5, 2, 923)
                ->addItem('Test Item 3', 15.55, 5, 42)
                ->addItem('Test Item 4', 1.25, 1, 923)
                ->addItem('Test Item 5', 3.12, 1, 3142)
                ->addItem('Test Item 6', 6.41, 3, 452)
                ->addItem('Test Item 7', 2.86, 1, 1526)
                ->addItem('Test Item 8', 5, 2, 923, 'https://dummyimage.com/64x64/000/fff')
                ->number(4021)
                ->with_pagination(true)
                ->duplicate_header(true)
                ->due_date(Carbon::now()->addMonths(1))
                ->notes('Lrem ipsum dolor sit amet, consectetur adipiscing elit.')
                ->customer([
                    'name'      => 'Èrik Campobadal Forés',
                    'id'        => '12345678A',
                    'phone'     => '+34 123 456 789',
                    'location'  => 'C / Unknown Street 1st',
                    'zip'       => '08241',
                    'city'      => 'Manresa',
                    'country'   => 'Spain',
                ])
                ->download('demo');

创建 Invoice 的单个实例并用项目填充它。然后调用 ->show()...不确定 ->show() 方法是否是您需要的,但您可以尝试使用它。

public function invoice($id)
    {
        $motorcycle_service = MotorcycleService::findOrFail($id);
        $invoice = \ConsoleTVs\Invoices\Classes\Invoice::make()
                ->number($motorcycle_service->id)
                ->with_pagination(true)
                ->duplicate_header(true)
                ->date(Carbon::parse($motorcycle_service->created_date))
                ->notes('Expected date and time to complete: ' . Carbon::createFromFormat('Y-m-d H:i:s', $motorcycle_service->expected_date)->format('d/m/Y h:i a'))
                ->customer([
                    'name'      => $motorcycle_service->motorcycle->customer->name,
                    'phone'     => $motorcycle_service->motorcycle->customer->phone_no,
                    'id'     => $motorcycle_service->motorcycle->customer->id,
                    'email' => $motorcycle_service->motorcycle->customer->email
                ]);

        foreach ($motorcycle_service->items as $item){
            $invoice->addItem($item->item->name, $item->item->price, $item->quantity, $item->id)            
        }

        return $invoice->show($motorcycle_service->id);
    }