php Laravel-eloquent:调用未定义的方法 Illuminate\Database\Eloquent\Collection::where()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22944937/
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-08-25 16:23:23  来源:igfitidea点击:

Laravel-eloquent: Call to undefined method Illuminate\Database\Eloquent\Collection::where()

phplaravel-4eloquent

提问by Martin Filek

I have two models in many-to-one relationship:

我有两个多对一关系的模型:

class Meal extends \Eloquent {
    /**
     * public Integer   $id; - primary key
     * public String    $name;
     */
    protected $fillable = array('id','name');

    public function mealProperties()
    {
        return $this->hasMany('MealProperty');
    }
}

class MealProperty extends \Eloquent {
    /**
     * public Integer   $id; - primary key
     * public Integer   $meal_id;
     */
    protected $fillable = array('id','meal_id');

    public function meal()
    {
        return $this->belongsTo('Meal', 'meal_id');
    }
}

if I ask for first meal first mealProperty everything go fine:

如果我要求第一餐第一餐财产一切顺利:

$mealProp = Meal::first()->mealProperties->first();

but if I ask for mealProperty with specific id of first meal this way:

但是,如果我以这种方式要求具有第一餐特定 id 的 mealProperty:

$mealProp = Meal::first()->mealProperties->where('id','=','1')->first();

I get this error:

我收到此错误:

Call to undefined method Illuminate\Database\Eloquent\Collection::where()

I google what I'm doing wrong two hours, but still nothing.

我用谷歌搜索了两个小时我做错了什么,但仍然没有。

If I can't use where method, what is possible way to get specific mealProperty?

如果我不能使用 where 方法,有什么可能的方法来获得特定的 mealProperty?

Thank you for help!

谢谢你的帮助!

回答by Jarek Tkaczyk

UPDATE for Laravel 5:

Laravel 5 更新:

Since v5 release there is a method whereon the Support\Collectionobject, so this question/answer becomes irrelevant. The method works exactly like filter, ie. returns filtered collection straight away:

由于 v5 版本whereSupport\Collection对象上有一个方法,所以这个问题/答案变得无关紧要。该方法的工作原理与filter,即。立即返回过滤后的集合:

$mealProp = Meal::first()->mealProperties->where('id','=','1'); // filtered collection

// that said, this piece of code is perfectly valid in L5:
$mealProp = Meal::first()->mealProperties->where('id','=','1')->first();


You must distinguish Laravel behaviour:

你必须区分 Laravel 的行为:

(dynamic property) Eloquent Collection or Model

(动态属性)雄辩的集合或模型

$meal->mealProperties

Relation Object

关系对象

$meal->mealProperties()

Now:

现在:

// mealProperties is Eloquent Collection and you call first on the Collection here
// so basically it does not affect db query
$mealProp = Meal::first()->mealProperties->first();

// here you try to add WHERE clause while the db query is already called
$mealProp = Meal::first()->mealProperties->where('id','=','1')->first();

// So this is what you want to do:
$mealProp = Meal::first()->mealProperties()->where('id','=','1')->first();

回答by The Alpha

You may try this:

你可以试试这个:

$mealProop1 = Meal::first()->mealProperties->find(1); // id = 1

Or something like this:

或者像这样:

$mealProops = Meal::first()->mealProperties;
$mealProop5 = $mealProops->find(5); // id = 5
$mealProop7 = $mealProops->find(7); // id = 7

Instead of this:

取而代之的是:

$mealProp = Meal::first()->mealProperties->where('id','=','1')->first();

Also, following should work:

此外,以下应该工作:

$mealProp = Meal::first()->mealProperties->first();