문제

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
도움이 되었습니까?

해결책

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();
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top