Reputation: 458
At the moment i have Query inside my Controller method made using Query Builder. 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. 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. 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. I just cant make sense of most of the documentation on this specific topic.
Thanks a lot.
Upvotes: 0
Views: 2282
Reputation: 458
Thanks to @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;
}
}
Upvotes: 0
Reputation: 6361
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);
Upvotes: 0
Reputation: 3755
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). 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. That's how I do it. if you want to learn more about the Repository pattern, here's a good article.
Upvotes: 1