count(): 参数必须是一个数组或者一个实现Countable in Laravel 6的对象

count(): Parameter must be an array or an object that implements Countable in Laravel 6

我想将来自多个 table 的数据显示到一个视图,第一个 table 是 Transaction_in,第二个 table 是 Transaction_in_detail 但在这两个旁边涉及其他 table。

这是Transcation_in控制器

class Transactions_inController extends Controller
{
public function show($id)
    {
        $supplierList = Supplier::where('id', 'nama')->first();
        $transactionin = Transaction_in::where('id', $id)->first();
        $deviceTypeList = DeviceType::where('id', 'nama_tipe_device')->first();
        $deviceBrandList = DeviceBrand::where('id', 'nama_brand_device')->first();
        $transactionindetail = Transaction_in_detail::where('id', 'Transansaction_in_id')->first();
        //return view('transactionsin.show', compact('supplierList', 'transactionsin', 'deviceTypeList', 'deviceBrandList', 'transactionindetail'));
        return view('transactionsin.show')->with('transactionsin', $transactionin);
        return view('transactionsin.show')->with('transactionsindetail', $transactionindetail);   
    }
}

Transaction_in型号

class Transaction_in extends Model
{
    protected $guarded = [];
    public function get_suppliers()
    {
        return $this->belongsTo(Supplier::class, 'Supplier_id');
    }
    public function get_devicetypes()
    {
        return $this->belongsToMany(DeviceType::class, 'DeviceType_id');
    }
    public function get_devicebrands()
    {
        return $this->belongsToMany(DeviceBrand::class, 'DeviceBrand_id');
    }
    public function get_transactionindetail()
    {
        return $this->belongsToMany(Transaction_in_detail::class, 'Transaction_in_id');
    }
}

Transaction_in_详细型号

class Transaction_in_detail extends Model
{
    protected $guarded = [];
    public function get_transction_in_id()
    {
        return $this->belongsTo(Transaction_in::class, 'Transaction_in_id');
    }
    public function get_devicetypes()
    {
        return $this->belongsToMany(DeviceType::class, 'DeviceType_id');
    }
    public function get_devicebrands()
    {
        return $this->belongsToMany(DeviceBrand::class, 'DeviceBrand_id');
    }
}

我想显示从 Transaction_in_detail table 到 Transaction_in 控制器的数据,但是我有这个错误

count(): Parameter must be an array or an object that implements Countable (View: C:\xampp\htdocs\inventory\resources\views\transactionsin\show.blade.php)

这是 transactionsin.show 代码 https://hastebin.com/ilewesucej.xml

您不需要return查看两次。使用 compact 设置多个变量。

public function show($id)
{
    $supplierList = Supplier::where('id', 'nama')->first();
    $transactionin = Transaction_in::where('id', $id)->first();
    $deviceTypeList = DeviceType::where('id', 'nama_tipe_device')->first();
    $deviceBrandList = DeviceBrand::where('id', 'nama_brand_device')->first();
    $transactionindetail = Transaction_in_detail::where('id', 'Transansaction_in_id')->first();
    //return view('transactionsin.show', compact('supplierList', 'transactionsin', 'deviceTypeList', 'deviceBrandList', 'transactionindetail'));
    return view('transactionsin.show',compact('transactionsin','transactionsindetail'));
}

在 blade 文件中检查 条件。

@if (!empty($transactionsin))
    <div class="tale-responsive">
        <table class="table table-hover table-bordered">
            <thead align="center">
                <tr class="table-primary">
                    <th>Tipe Perangkat</th>
                    <th>Brand Perangkat</th>
                    <th>Spesifikasi</th>
                    <th>Jumlah</th>
                    <th>Harga</th>
                    <th>Total Harga</th>
                </tr>
            </thead>
            <tbody>

                <tr>
                    <td>{{ $transactionin->get_devicetypes->nama_tipe_device }}</td>
                    <td>{{ $transactionin->get_devicebrands->nama_brand_device }}</td>
                    <td>{{ $transactionin->get_transactionindetail->spek_device }}</td>
                    <td>{{ $transactionin->get_transactionindetail->harga_device }}</td>
                    <td>{{ $transactionin->get_transactionindetail->jumlah_device }}</td>
                    <td>{{ $transactionin->get_transactionindetail->total_harga_device }}</td>
                </tr>

            </tbody>
        </table>
    </div>
@else
    <h6>No Data Found</h6>
@endif

1. 您正在使用 first() 方法,该方法 return 是单个 object ,而不是数组。 return 方法不是 return 模型集合,而是 first() 方法 return 单个模型实例。按如下方式重写您的代码 -

$transactionin = Transaction_in::where('id', $id)->get();

供参考,Laravel Docs

2. 您不必 return 查看和变量两次。 Return一次如下 -

return view('transactionsin.show',compact('transactionsin','transactionsindetail'));

您的 table 设置是什么样的,具体来说 Transaction_inTransaction_in_detail 之间的关系是什么? Transaction_in_detail::get_transaction_in_id 中给出了一对多关系,Transaction_in::get_transactionindetail 中给出了多对多关系。

belongsToMany 关系适用于具有枢轴 table 的多对多关系。也许这应该是 hasMany 而不是?

首先要正确地阐明这种关系。参见 the docs on relationships in Laravel。然后就可以拉取正确的数据了。


您是否正在尝试获取 ID 为 $id 的一个 Transaction_in 实例?在那种情况下,不,你不能迭代它。也许您正在尝试这样的事情?

$transactionin = Transaction_in::find($id);
$transactionindetail = $transactionin->get_transactionindetail;

// $transactionindetail will now be a `Collection` which implements `Countable`.

另请注意,您(以及其他一些答案)混淆了 transactionsintransactionin(没有 's')。

如果你想让对象可数,你必须使用 $object->count()你的情况 $transactionin->count()