简体   繁体   English

在模型中的数据库查询生成器中使用控制器变量-Laravel

[英]Using Controller Variable in DB Query Builder in a Model - Laravel

At the moment i have Query inside my Controller method made using Query Builder. 目前,我已经在使用查询生成器的Controller方法中查询了。 Like so: 像这样:

 public function postFilters(){
    $filt = Input::get('name');
    $query = DB::table('wm.top_pages')->where('filter',$filt)->limit(20)->get();

    return View::make('topPages.table', ['wm'=>$query]);
} 

I would like to get the query outside of Controller and into my Model. 我想查询查询以外的Controller并进入我的模型。 But everytime i do this, i get an error because the same query as above, but now inside my model does not recognize the variable $filt, which actually gotten from my View. 但是每次我这样做时,都会出现错误,因为与上面相同的查询,但是现在我的模型内部无法识别变量$ filt,该变量实际上是从View获取的。 Can anyone advise on how to go about this? 谁能建议如何解决这个问题? What i have done above is actually not what MVC should look like and i'm assuming there are ways of getting around it. 我在上面所做的实际上不是MVC的样子,我假设有一些解决方法。 I just cant make sense of most of the documentation on this specific topic. 我只是无法理解有关该特定主题的大多数文档。

Thanks a lot. 非常感谢。

Well if I were you, I would create a Repository interface, and create an implementation to it, in this implementation I would add all my queries, and then the variable filter would be passed as an argument, then in my controller I would inject my interface (which is really simple using laravel). 好吧,如果我是您,我将创建一个Repository接口,并为其创建一个实现,在该实现中,我将添加所有查询,然后将变量过滤器作为参数传递,然后在控制器中注入我的界面(使用laravel真的很简单)。 So I end up with an application which is extensible, if I ever want to switch from MySQL to another database all I have to do is to create a new implementation and configure laravel to use the new implementation. 因此,我最终得到了一个可扩展的应用程序,如果我想从MySQL切换到另一个数据库,我要做的就是创建一个新的实现并配置laravel以使用该新实现。 That's how I do it. 这就是我的方法。 if you want to learn more about the Repository pattern, here's a good article. 如果您想了解更多关于Repository模式的信息, 这是一篇很好的文章。

I do not know why you are sending a query to the model let whatever the reason be following is the code : 我不知道您为什么向模型发送查询,无论原因为何,代码如下:

in your model 在您的模型中

<?php 
 class SomeModel extends Eloquent
 {
 public static function funcname($filter,$data)
 {
 // the filter passed from controller is in $filter
 }
 }

in your controller 在您的控制器中

 $filt = Input::get('name');
 $query = DB::table('wm.top_pages')->where('filter',$filt)->limit(20)->get();

 //pass the data from this controller to funcname function we created in model
 SomeModel::funcname($filt,$query);

Thanks to @Elias Van Ootegem: 感谢@Elias Van Ootegem:

I passed on the variable as a parameter/argument to the model function and it worked. 我将变量作为参数/参数传递给了模型函数,并且它起作用了。 Controller: 控制器:

 public function postFilters(){
   $filt = Input::get('name');
   $query = webmasters::getFilters($filt);
   return View::make('topPages.table', ['webmasters'=>$query]);
 } 

Model: 模型:

class webmasters {
   public static function web($filt) {
   $top_pages = DB::table('wm.top_pages')->where('filter',$filt)->limit(20)->get();
   return $top_pages;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM