小编Mar*_*lek的帖子

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

我有两种模式,多对一关系:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我要求第一餐第一餐属性一切顺利:

$mealProp = Meal::first()->mealProperties->first();
Run Code Online (Sandbox Code Playgroud)

但是,如果我用这种方式询问具有第一餐特定id的mealProperty:

$mealProp = Meal::first()->mealProperties->where('id','=','1')->first();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Call to undefined method Illuminate\Database\Eloquent\Collection::where()
Run Code Online (Sandbox Code Playgroud)

我两个小时谷歌我做错了什么,但仍然没有.

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

谢谢你的帮助!

php eloquent laravel-4

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

eloquent ×1

laravel-4 ×1

php ×1