Laravel 将参数从控制器传递给模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26058830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:10:43  来源:igfitidea点击:

Laravel pass parameters from controller to Model

laravellaravel-4eloquent

提问by Yunus Aslam

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 :

我是 laravel 的新手,我试图将参数从控制器中的函数传递到模型中的函数。我已经这样做了:

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"

我收到错误信息 "Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string"

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

如何将参数从我的控制器传递到模型?

采纳答案by Jarek Tkaczyk

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

范围要求query实例是第一个参数(它会自动传递给函数):

// 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 scopepart should be uppercase.

只是关于方法名称的注释 - 它应该遵循一个规则 -scope部分后面的第一个字母应该是大写的

回答by Marcin Nabia?ek

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

您应该将函数名称从 更改scopeyesterdaysorderdetailsyesterdaysorderdetails。似乎作用域是特殊函数,它们只接受一个参数$query,这可能是 Eloquent Builder,这就是您收到此错误的原因。

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 scopeI think

而在你的情况下,你只是想显示2个日期与分开的--,所以你不需要示波器功能为,只是正常的功能,可以不首先scope我想