我有个这样的模特:
class Event extends Eloquent
    {
        protected $softDelete = true;
        public function scopeSearchEvents($search_criteria)
        {
            return Event::whereIn('title',$search_criteria)
                            ->orWhereIn('description',$search_criteria)
                            ->whereApproved('1')
                            ->orderBy('event_date','desc')
                            ->get();
        }
    }我从控制器呼叫它,就这样:
$data = Event::search($search_criteria);但是它给出了这样的错误:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method Illuminate\Events\Dispatcher::search()从控制器调用自定义模型方法的最佳方法是什么?
发布于 2014-05-25 01:05:13
对方法进行更改,如下所示:
public function scopeSearchEvents($query, $search_criteria)
{
    return $query->whereIn('title', $search_criteria)
                 ->orWhereIn('description', $search_criteria)
                 ->whereApproved('1')
                 ->orderBy('event_date','desc');
}然后把它叫做searchEvents而不是search
// Don't use Event as your model name
$data = YourModel::searchEvents($search_criteria)->get();还请确保,您希望使用whereIn而不是where('title', 'LIKE', "% $search_criteria")等等。
更新:
您应该将模型名从Event更改为任何其他类型,因为Laravel有它的核心Event类,实际上是映射到'Illuminate\Support\Facades\Event'的Facade。
发布于 2014-10-30 15:05:23
看看app.php
'aliases' => array(
        ...
        'Event'=> 'Illuminate\Support\Facades\Event',
        ...
);"事件"被定义为别名。这就是为什么你的事件调用事件现在,如果您想使用事件模型而不键入名称空间来调用方法,请创建一个别名,如下所示:
'MyEvent' => 'App\Models\Event',然后:
MyEvent::create();https://stackoverflow.com/questions/23850436
复制相似问题