Skip to content
Advertisement

Put data in the method (addItem) using foreach

Code i tried but it display 1 value. I want to use addItem to insert more data and display it on my invoice pdf. I can grab all my data using eloquent but dont know how to insert into the class given.

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

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

            $invoice = ConsoleTVsInvoicesClassesInvoice::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;
    }

Example code at https://github.com/ConsoleTVs/Invoices

$invoice = ConsoleTVsInvoicesClassesInvoice::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');

Advertisement

Answer

Create single instance of Invoice and fill it with items. Then call ->show()… not sure is the ->show() method that you need but you can experiment with it.

public function invoice($id)
    {
        $motorcycle_service = MotorcycleService::findOrFail($id);
        $invoice = ConsoleTVsInvoicesClassesInvoice::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);
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement