码迷,mamicode.com
首页 > 其他好文 > 详细

laravel orm

时间:2018-10-16 16:04:02      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:pos   variable   nts   return   nbsp   bsp   turn   xtend   name   

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

标签:pos   variable   nts   return   nbsp   bsp   turn   xtend   name   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!