首页 > 其他 > 详细

laravel orm

时间:2018-10-16 15:33:19      阅读:123      评论:0      收藏:0      [点我收藏+]

https://laravelacademy.org/post/9584.html

一对一:


class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

class Profile extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

 

一对多:

 class Post extends Model { 
  public function comments() {
   return $this->hasMany(Comment::class);
   } }

class Comment extends Model {
   public function post() {
     return $this->belongsTo(Post::class);
     } }

多对多:


class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

class Role extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

 

laravel orm

原文:https://www.cnblogs.com/lxwphp/p/9797869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!