Laravel: Problem with foreach

Asked

Viewed 251 times

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 from the bug:

"Trying to get Property '***' of non-object (View: C: Users grosa Desktop Laravel siscom Resources views occupation Extract.blade.php)".

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

  • Give a balcony like the object is leaving your controller a "dd" on it after the query runs

  • If you are selecting only one record, find(1), why do you want to do the foreach?

  • True, I forgot I was with find(1), but when I put the get() it runs from dd() quiet, but in Blade it gives the following error "Property [id] does not exist on this Collection instance."

  • And what was the exit from dd? Put to the question...

1 answer

0

So I managed to solve 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 specified in Controller only what I needed in @foreach.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.