Laravel 4 - 查询生成器 - 无法使用分页和clausole

时间:2014-06-30 13:47:03

标签: php laravel-4 eloquent query-builder

我有一个应用程序需要根据用户的动态选择从数据库中获取数据 这是我的控制器代码:

$invoice_query = DB::table('invoices')->select('invoices.title', 'invoices.created_at', 'invoices.id', 'invoices.status', 'clients.first_name', 'clients.last_name')->join('clients', 'invoices.client_id', '=', 'clients.id');

    if( Input::has('status') && ( $status = Input::get('status') ) != "" )
    {
        $invoice_query->where('status', '=', $status);
    }

    if( Input::has('client') && ( $client_id = Input::get('client') ) != ""  )
    {
        $invoice_query->where('client_id', '=', $client_id);
    }


    $invoice_query->paginate(1);


    $data['invoices'] = $invoice_query->get();

    $data['total_invoices'] = 4;

    $this->layout->content    = View::make('backend.invoices.index', $data);

这个观点:

@foreach( $invoices as $invoice )
                        <tr>
                            <td>{{ $invoice->created_at }}</td>
                            <td>{{ $invoice->first_name }} {{ $invoice->last_name }}</td>
                            <td>{{ $invoice->title }}</td>
                            <td>
                                @if( $invoice->status == 0 )
                                    <span style="color: red; font-weight: bold;">Da pagare</span>
                                @else
                                    <span style="color: green; font-weight: bold;">Pagato</span>
                                @endif
                            </td>
                            <td>
                                <div class="btn-group">
                                    <a class="btn btn-warning" style="color: #FFF;" href="{{ url('invoices/'.$invoice->id.'/edit') }}">Modifica</a>
                                    <a class="btn btn-danger" style="color: #FFF;" href="{{ url('invoices/'.$invoice->id) }}">Cancella</a>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                {{ $invoices->links() }}

问题是我得到的控制器代码:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to a member function links() on a non-object

我认为问题可能是:

$data['invoices'] = $invoice_query->get();

所以我删除了->get()但在这种情况下我得到了:

ErrorException

Trying to get property of non-object (View: /var/www/fatture/app/views/backend/invoices/index.blade.php)

无法弄清楚出了什么问题,能帮帮我吗?

NB:
如果我使用:

$data['invoices'] = Invoices::join('clients', 'invoices.client_id', '=', 'clients.id')->paginate( 1, array('invoices.title', 'invoices.created_at', 'invoices.id', 'clients.first_name', 'clients.last_name') );

一切正常但我不知道如何添加where

1 个答案:

答案 0 :(得分:0)

问题是将值赋给变量 使用replace

删除 - &gt; get()和paginate
 $data['invoices'] = $invoice_query->paginate(1);