Question

In my view I list on the flow table rows from parsed xml which contains a specific mysql table ID and I would like to use it to count existing rows in my table. How would I use a helper function in laravel view which expect this id and return counted rows?

something like

@foreach ($items as $item)

counted:{{count_rows($item->id)}}

@endforeach
Était-ce utile?

La solution

You should not do this kind of calculations in your view, this is something your models should be doing, or, maybe, you controller. This is an example:

class Billing extends Controller {

    public function show($id)
    {
        $billing = Billing::find($id);

        return View::make('billing.show')->with('billing', $billing->getAllWithItems())
    }

}

class Billing extends Eloquent {

    public function getAllWithItems() 
    {
        return DB::table('billing')
            ->select(DB::raw('COUNT(items.id) as item_count')))
            ->left_join('items', 'items.billing_id', '=', 'billing.id')
            ->group_by('items.id')
            ->get();
    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top