【问题标题】:Laravel 4 - Query Builder - not able to use pagination and where clausoleLaravel 4 - 查询生成器 - 无法使用分页和 where clausole
【发布时间】:2014-06-30 13:47:02
【问题描述】:

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

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

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

错误异常

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

不知道怎么回事,能帮帮我吗?

注意:
如果我使用:

$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') );

一切正常,但我不知道如何添加位置

【问题讨论】:

    标签: php laravel-4 eloquent query-builder


    【解决方案1】:

    问题在于将值分配给变量 删除 ->get() 和 replacepaginate

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

    【讨论】:

    • 当我们犯了一个小错误时就会发生这种情况:)
    猜你喜欢
    • 2014-03-13
    • 2018-01-03
    • 1970-01-01
    • 2018-12-26
    • 2019-05-06
    • 2014-10-16
    • 2014-04-10
    • 2016-02-14
    • 2013-06-28
    相关资源
    最近更新 更多