phplaraveleloquentpagination

Laravel 4, Eloquents pagination method


Hello! I am having a problem when trying to use the paginate method on an eloquent model. The problem being that calling {{ $model->links() }} in my view file will throw me a Call to undefined method Illuminate\Database\Query\Builder::links() which didn't really get me anywhere due to it working perfectly on another view file.

Controller.php

$data = Routine::whereHas('measurements', function($q)
                {
                    $fra = str_replace("/","-", Input::get('fraDato'));
                    $fra = date('Y-m-d', strtotime($fra));
                    $til = str_replace("/","-", Input::get('tilDato'));
                    $til = date('Y-m-d', strtotime($til));

                    $q->whereBetween('date', array($fra, $til));
                })->with('emps')->orderBy('date', 'time', 'title')->paginate(5);

                return View::make('sok')
                ->with('title', 'Søk')
                ->with('date', $date)
                ->with('data', $data);

sok.blade.php

@if(isset($data))
        <table>
            <tr>
                <td>ID</td>
                <td>Tittel</td>
                <td>Verdi</td>
                <td>Ansatt ID</td>
                <td>Dato</td>
                <td>Tid</td>
                <td>Basseng</td>
            </tr>
            @foreach($data as $data)
                <tr>
                    <td>{{ $data->id }}</td>
                    <td>{{ $data->measurements[0]->title }}</td>
                    <td>{{ $data->value }}</td>
                    <td>{{ $data->emps->user_name }}</td>
                    <td>{{ date('d/m/Y', strtotime($data->date)) }}</td>
                    <td>{{ $data->time }}</td>
                    @foreach($data->measurements as $measurement)
                        <td>{{ $measurement->pivot->pool_id }}</td>
                    @endforeach
                    <td>{{ HTML::linkRoute('edit_data', 'Rediger', $data->id) }}</td>
                    <td>{{ HTML::linkRoute('delete_confirm', 'Slett', $data->id) }}
                    </td>
                </tr>
            @endforeach
                   {{ $data->links() }}

Thanks!


Solution

  • You override $data in you foreach:

     @foreach($data as $data)
    

    change it to eg.

     @foreach($data as $dataItem)
    

    And I'd suggest using meaningful variable names, so you won't have problems like this. $data is so generic, you could name everything with that. use $routines instead.