laravel 5.6发票管理器-在字符串上调用成员函数count()

时间:2018-07-19 14:14:54

标签: php html laravel laravel-5 invoice

我正在laravel 5.6中开发一个发票管理器应用程序。 我正在尝试遵循以下指南:https://www.youtube.com/watch?v=h6sTdAX6yTs&list=PLVAw_4sB6qJwCloLkV1SudLR0wcgiEXay&index=2 但是我陷入了需要在索引(8:00)中显示发票的部分。

错误:在字符串上调用成员函数count()

index.blade.php:

@extends('layouts.app')

@section('content')

<div>
    <div>
        <div>
            <span>Invoices</span>
            <a href="{{route('invoices.create')}}">Create</a>
        </div>
    </div>
    <div>
        @if(($invoices->count()))
            <table>
                <thead>
                    <th>invoice no.</th>
                    <th>Grand Total</th>
                    <th>Client</th>
                    <th>Invoice Date</th>
                    <th>Due Date</th>
                    <th colspan="2">Created At</th>
                </thead>
                <tbody>
                    @foreach($invoices as $invoice)
                    <tr>
                        <td>{{$invoice->invoice_no}}</td>
                        <td>{{$invoice->grand_total}}</td>
                        <td>{{$invoice->client}}</td>
                        <td>{{$invoice->due_date}}</td>
                        <td>{{$invoice->created_at->diffForHumans()}}</td>
                        <td></td>
                    </tr>
                    @endforeach
            </table>
        @else
            <div>
                <p>
                    No Invoices were created.
                    <a href="{{route('invoices.create')}}">Create now!</a>
                </p>
            </div>
        @endif
    </div>
</div>
@endsection

InvoiceController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\InvoiceProduct;
use App\Invoice;
use DB;

class InvoiceController extends Controller
{
    public function index(){
        $title = 'Welkom';
        //return view('pages.index', compact('title'));
        $invoices = DB::select('SELECT * FROM invoices');
        return view('invoices.index')->with('invoices', $title);
    }
}

2 个答案:

答案 0 :(得分:3)

with函数接受两个参数,一个键和一个值。在您的情况下,您已将"invoice"键设置为字符串$title的值。如果要包含多个参数,则可以使用类似的关联数组:

return view('invoices.index')->with([
    'invoices' => $invoices, 
    'title' => $title
]);

使用compact函数可以使时间更短:

return view('invoices.index')->with(compact('invoices', 'title'));

答案 1 :(得分:0)

始终使用compact从控制器传递变量以查看它更简单的方法。您可以使用以下任何代码。

使用雄辩的第一种方法:

 public function index(){
        $title = 'Welkom';
        //return view('pages.index', compact('title'));
        $invoices = Invoice::all();
        return view('invoices.index',compact('title','invoices'));
    }

使用查询生成器的第二种方式:

  use Illuminate\Support\Facades\DB; 

  public function index(){
        $title = 'Welkom';
        //return view('pages.index', compact('title'));
        $invoices = DB::select('SELECT * FROM invoices');
        return view('invoices.index',compact('title','invoices'));
    }

再次使用查询生成器的第三种方式:

 use Illuminate\Support\Facades\DB; 

 public function index(){
        $title = 'Welkom';
        //return view('pages.index', compact('title'));
        $invoices = DB::table('invoices')->get();
        return view('invoices.index',compact('title','invoices'));
    }