0

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.

2
  • 1
    Passing it as an argument springs to mind here... That's what they're for after all Commented Dec 8, 2014 at 10:15
  • Thank you. Was right in front of my nose the entire time. Commented Dec 8, 2014 at 13:58

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Even though this is better practice. I chose to go the easier way of passing the variable on to the model function as a parameter. It is afterall my initial laravel project and i'm getting used to the nuts and bolts of it.
You made the good decision starting with Laravel, you'll learn a powerfull framework and a lot fo good practices as you dive in deeper. Good luck @TonyOkoth.
0

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

1 Comment

I'm not passing anything to my model in my code above. What i would like to do is pass the variable '$filt' to a query in my model as a where-clause.
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.