2015-04-14 136 views
1

为什么会按预期返回集合;Laravel无法正常工作

$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

没有必要whereIn()之前调用all()。你的代码改成这样:

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

可以在Eloquent basic usage文档找到更多的例子。