Laravel 关系

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

Laravel Relationships

laravelbelongs-torelationshipslaravel-4eloquent

提问by Gareth Daine

I've been looking over relationships in Laravel 4 in the documentationand I'm trying to work out the following.

我一直在查看文档中 Laravel 4 中的关系,我正在尝试解决以下问题。

I have a table in my database called 'events'. This table has various fields that mainly contain ID's that relate to other tables. For example, I have a 'courses' table. The events table contains a field called 'course_id' which relates to the ID of the 'id' field in the courses table.

我的数据库中有一个名为“事件”的表。该表有各种字段,主要包含与其他表相关的 ID。例如,我有一个“课程”表。事件表包含一个名为“course_id”的字段,它与课程表中“id”字段的 ID 相关。

So basically, I'm after some advice on how you go about relating the two (belongsTo()?) and then passing the connected data to the view.

所以基本上,我对如何将两者关联起来(belongsTo()?),然后将连接的数据传递给视图有一些建议。

Here is where I am at so far http://paste.laravel.com/pf3.

到目前为止,这是我所在的位置http://paste.laravel.com/pf3

I hope you guys are able to give me some advice on how best to approach this problem. Thanks.

我希望你们能够就如何最好地解决这个问题给我一些建议。谢谢。

Gaz

加斯

回答by Dave Amphlett

This is basically the same answer as @Marko Aleksi?, but with the hasOne() and belongsTo() relationships the right way around.

这与@Marko Aleksi? 的答案基本相同,但使用 hasOne() 和belongsTo() 关系是正确的。

class Course extends Eloquent{

    protected $table = 'courses';


    public function event()
    {
        return $this->hasOne('Event'); // links this->id to events.course_id
    }
}


class Event extends Eloquent {

    protected $table = 'events';

    public function course()
    {
        return $this->belongsTo('Course'); // links this->course_id to courses.id
    }

}

回答by Marko Aleksi?

You need to specify in both models a relationship. belongsTo() in one, hasOne() in the other, since you are using one-to-one relationship

您需要在两个模型中指定一个关系。一个是belongsTo(),另一个是hasOne(),因为你使用的是一对一的关系

class Course extends Eloquent{

    protected $table = 'courses';


    public function event()
    {
        return $this->belongsTo('Event');
    }
}


class Event extends Eloquent {

    protected $table = 'events';

    public function course()
    {
        return $this->hasOne('Course');
    }

}

Then calling it in the route or the controller would be as following:

然后在路由或控制器中调用它如下:

Course of specific event (in this case with id of 1)

特定事件的进程(在这种情况下,id 为 1)

$course = Event::find(1)->course;

Event of specific course (in this case with id of 1)

特定课程的事件(在这种情况下,id 为 1)

$event = Course::find(1)->event;

Please refer to the Laravel 4 documentation, section Eloquent ORM: http://laravel.com/docs/eloquent#one-to-one

请参阅 Laravel 4 文档,Eloquent ORM 部分:http://laravel.com/docs/eloquent#one-to-one