Laravel WhereIn Not Working

时间:2015-04-14 12:59:48

标签: php laravel eloquent laravel-5

为什么这会按预期返回集合;

$postcodes = DB::table('payments')->whereIn('VendorZIP', array('BS19AA','PO48AA'))->get();
print_r($postcodes);

但这会返回错误;

$payments = Payment::all();
$payments->whereIn('VendorZIP', array('BS19AA','PO48AA'))->get();
print_r($payments);

错误是;

Call to undefined method Illuminate\Database\Eloquent\Collection::whereIn()

我当然有付款模式;

<?php namespace App\Models;

  use Illuminate\Database\Eloquent\Model;

  class Payment extends Model {
    //
  }

1 个答案:

答案 0 :(得分:2)

无需在all()之前致电whereIn()。将您的代码更改为:

$payments = Payment::whereIn('VendorZIP', array('BS19AA','PO48AA'))->get();

您可以在Eloquent basic usage文档中找到更多示例。