phplaraveleloquentlaravel-4

Laravel pass parameters from controller to Model


I am new in laravel and I am trying to pass parameters from a function in controller to a function in model. I have done this :

My controller

class DashBoardController extends BaseController {
    public function yesterdayOrderData(){
        $from = date('Y-m-d H:i:s', strtotime('today -1 days'));
        $to = str_replace("00:00:00", "23:59:59", $from);
        $odata = Invoice::yesterdaysorderdetails($from, $to);
    }

}

My Model :

class Invoice extends Eloquent{
    protected $table = 'Inf_Invoice';
    public function scopeyesterdaysorderdetails($from, $to){
        echo $from."--".$to;
    }
}

I am getting error message "Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string"

How can I pass parameters from my controller to model ??


Solution

  • Scope requires query instance to be 1st param (it's automatically passed to the function):

    // you don't need to use camelCase, but it's soooo hard to read this otherwise..
    public function scopeYesterdaysOrderDetails($query, $from, $to){
         $query->whereBetween('created_at', [$from, $to]);
    }
    
    
    // usage:
    $odata = Invoice::yesterdaysOrderDetails($from, $to)->get();
    

    Just a note about the method name - it should follow one rule - first letter after the scope part should be uppercase.