Laravel: Problem with foreach

0
public function extract(){
    $occupation = Occupation::join('invoices', 'occupations.id', '=', 'invoices.occupation_id')
        ->leftJoin('payments', 'invoices.id', '=', 'payments.invoice_id')
        ->select('occupations.*',
            'invoices.occupation_id',
            'invoices.id as invoiceId',
            'invoices.date_invoice',
            'invoices.date_pay',
            'invoices.ufir as invoiceUfir',
            'payments.date_payment',
            'payments.value')
        ->find(1);
    return view('occupation/extract', ['occupation' => $occupation]);
}

This is my Controller.

@foreach($occupation as $o)
<tr>
    <td>{{ $o->invoiceId }}</td>
    <td>{{ date('m/Y', strtotime($o->date_invoice)) }}</td>
    <td>{{ date('d/m/Y', strtotime($o->date_pay)) }}</td>
    <td>{{ number_format(($o->invoiceUfir), 2, ',', '.') }}</td>
    <td></td>
    <td></td>
    <td></td>
    <td>{{ date('d/m/Y', strtotime($o->date_payment)) }}</td>
    <td>{{ number_format($o->value, 2, ',', '.') }}</td>
    <td></td>
</tr>
@endforeach

This is my Blade.

When I put it without @foreach it works perfectly, now when I put it to bring the other information of the error:

"Trying to get property '***' of non-object (View: C: \ Users \ gros \ Desktop \ Laravel \ siscom \ resources \ views \ occupation \ extract.blade.php) >

I look forward to your support. Thank you in advance for your cooperation.

    
asked by anonymous 11.06.2018 / 21:34

1 answer

0

So I solved the problem.

public function extract(Occupation $occupation){
    $occupations = Occupation::join('invoices', 'occupations.id', '=', 'invoices.occupation_id')
        ->leftJoin('payments', 'invoices.id', '=', 'payments.invoice_id')
        ->select('occupations.id',
            'invoices.occupation_id',
            'invoices.id as invoiceId',
            'invoices.date_invoice',
            'invoices.date_pay',
            'invoices.ufir as invoiceUfir',
            'payments.date_payment',
            'payments.value')
        ->get();
    return view('occupation/extract', compact('occupation', 'occupations'));
}

The Controller.

@foreach($occupations as $o)
<tr>
    <td>{{ $o->invoiceId }}</td>
    <td>{{ date('m/Y', strtotime($o->date_invoice)) }}</td>
    <td>{{ date('d/m/Y', strtotime($o->date_pay)) }}</td>
    <td>{{ number_format(($o->invoiceUfir), 2, ',', '.') }}</td>
    <td></td>
    <td></td>
    <td></td>
    <td>{{ date('d/m/Y', strtotime($o->date_payment)) }}</td>
    <td>{{ number_format($o->value, 2, ',', '.') }}</td>
    <td></td>
</tr>
@endforeach

The Blade.

I have specified in the Controller only what I needed in @foreach.

    
11.06.2018 / 22:04