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

laravel 5.6 invoice manager - Call to a member function count() on string

我正在 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);
    }
}

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

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

使用 compact 函数可以更短:

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

始终使用 compact 从控制器传递变量以查看它更容易way.you可以使用以下任何代码。

第一种方式使用 eloquent:

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