drphfy1198 2014-05-24 22:56
浏览 135
已采纳

从控制器laravel4调用自定义模型方法

I have a model like this:

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

And im calling it from the controller like this:

$data = Event::search($search_criteria);

But it gives this error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method Illuminate\Events\Dispatcher::search()

What is the best way of calling a custom model method from your controller?

  • 写回答

2条回答 默认 最新

  • douchen4534 2014-05-25 01:05
    关注

    Make changes to your method as given below:

    public function scopeSearchEvents($query, $search_criteria)
    {
        return $query->whereIn('title', $search_criteria)
                     ->orWhereIn('description', $search_criteria)
                     ->whereApproved('1')
                     ->orderBy('event_date','desc');
    }
    

    Then call it like searchEvents not search:

    // Don't use Event as your model name
    $data = YourModel::searchEvents($search_criteria)->get();
    

    Also make sure that, you want to use whereIn instead of where('title', 'LIKE', "% $search_criteria") and so.

    Update:

    You should change the model name from Event to anything else because Laravel has it's core Event class, actually a Facade which is mapped to 'Illuminate\Support\Facades\Event'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误