与Laravel Eloquent一起加入桌子

时间:2014-09-25 15:05:50

标签: laravel eloquent

我有两个表,testimonialsarea

Testimonialsarea一对多关系。我基本上想用 Eloquent 来创建以下 SQL 查询:

SELECT * FROM 'testimonial' INNER JOIN area ON 'area_id' = area.id

我的模特看起来像这样:

class Area extends \Eloquent {


    protected $table = 'area';

    public function testimonials()
    {
        return $this->hasMany('Testimonials');
    }


}

class Testimonial extends \Eloquent {


    protected $table = 'testimonial';

    public function area()
    {
        return $this->belongsTo('Area');
    }

}

我认为在我的路线中我可以这样做以返回所有testimonial及其area s

$testimonial = Testimonial::all();

然而它没有按预期工作。这应该很简单,但我尝试的一切都没有用。

1 个答案:

答案 0 :(得分:0)

尝试:

Testimonial::with('area')->get();
相关问题