Yunus Aslam
Yunus Aslam

Reputation: 2466

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 ??

Upvotes: 2

Views: 8200

Answers (2)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81147

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.

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You should change your function name from scopeyesterdaysorderdetails to yesterdaysorderdetails. It seems scopes are special functions and they take only one parameter $query which is probably Eloquent Builder and that's why you get this error.

And in your situation you just want to display 2 dates separated with -- so you don't need scope functions for that, just normal function that cannot begin with scope I think

Upvotes: 0

Related Questions