laravel ORM模型关联

 /**
*适用于一对一、 一对多
*第一个参数为关联模型
*第二个参数为关联模型关联字段
*第三个参数为本地模型关联的字段
**/
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');


/**
* 逆向(一对一、 一对多)
*Eloquent 将会尝试通过Phone模型的user_id去User模型查找与之匹配的记录。Eloquent 通过关联关*系方法名并在方法名后加_id后缀来生成默认的外键名。
**/
return $this->belongsTo('App\User', 'foreign_key', 'other_key');


/**多对多**/
//这里要注意,Role前必须加APP\,还有就是第二个参数是表名,数据库里那个表叫啥,
//他就叫啥,第三个参数是本类的字段,第四个参数是另一个模型在连接表里的外键名
return $this->belongsToMany('App\Role','role_user','user_id','role_id');


/**获取中间表字段**/
$user = App\User::find(1);
foreach ($user->roles as $role) {
    echo $role->pivot->created_at;
}


默认情况下,pivot 对象只包含两个关联模型的主键,如果你的中间表里还有其他额外字段,你必须在定义关联时明确指出:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');

在定义关系时,你还可以使用 wherePivot 和 wherePivotIn 方法来过滤 belongsToMany 返回的结果:
return $this->belongsToMany('App\Role')->wherePivot('approved', 1);
return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);


 /**
*远程一对一、一对多
*第一个参数是希望访问的模型名称   
*第二个参数是中间模型的名称
*第三个参数表示中间模型的外键名
*第四个参数表示最终模型的外键名
*第五个参数表示本地键名
*第六个参数表示中间模型的本地键名
*供应商可以通过用户访问用户的历史记录
 *users(id、supplier_id)suppliers(id)  history(id、user_id)
**/
class Supplier extends Model
{
//一对一  
    public function userHistory()
    {
//最终被关联的模型,中间模型,中间模型的外键,最终模型的外键
        return $this->hasOneThrough(
            'App\History', 
            'App\User',
            'supplier_id', // 用户表外键
            'user_id', // 历史记录表外键
            'id', // 供应商本地键
            'id' // 用户本地键
        );
    }
}

class Country extends Model
{
//一对多
    public function posts()
    {
        return $this->hasManyThrough(
            'App\Post',
            'App\User',
            'country_id', // 用户表外键
            'user_id', // 文章表外键
            'id', // 国家表本地键
            'id' // 用户表本地键
        );
    }
}


 /**
 *一对一(多态)
 *表:posts(id,name ) users(id,name ) images(id,url,imageable_id,imageable_type)
 *imageable_id 列包含文章或用户的 ID 值,imageable_type 列来判断父模型的 「类型」
**/
class Image extends Model
{
    /**
     * 获取拥有此图片的模型。
     */
    public function imageable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * 获取文章图片。
     */
    public function image()
    {
        return $this->morphOne('App\Image', 'imageable');
    }
}

class User extends Model
{
    /**
     * 获取用户图片。
     */
    public function image()
    {
        return $this->morphOne('App\Image', 'imageable');
    }
}


 /**
 *一对多(多态)(一对多关联类似)
 *表:posts(id,title,body) videos(id,title,url ) comments(id,bod,commentable_id,commentable_type)
 *commentable_id 列包含文章或视频的 ID 值,commentable_type 列来判断父模型的 「类型」
**/
class Comment extends Model
{
    /**
     * 获取拥有此评论的模型。
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * 获取此文章的所有评论。
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

class Video extends Model
{
    /**
     * 获取此视频的所有评论。
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

//获取关联
$post = App\Post::find(1);

foreach ($post->comments as $comment) {
    //
}


 /**
 *多对多(多态)
*除了传统的多态关联,还可以定义“多对多”的多态关联,例如,一个博客的 Post 和 Video 模型可能
*共享一个 Tag模型的多态关联。使用对多对的多态关联允许你在博客文章和视频之间有唯一的标签列*表。
**/
posts
    id - integer
    name - string

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

第一步先在Post和videos中添加morphToMany 方法:
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model{
    /**
     * 获取指定文章所有标签
     */
    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}


namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model{
    /**
     * 获取指定文章所有标签
     */
    public function tags()
    {
        return $this->morphToMany('App\Tag', 'taggable');
    }
}


然后在Tag模型中每一个关联模型定义一个方法,例如,我们定义一个posts方法和videos方法:
namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model{
    /**
     * 获取所有分配该标签的文章
     */
    public function posts()
    {
        return $this->morphedByMany('App\Post', 'taggable');
    }

    /**
     * 获取分配该标签的所有视频
     */
    public function videos()
    {
        return $this->morphedByMany('App\Video', 'taggable');
    }
}


使用时:
$post = App\Post::find(1);

foreach ($post->tags as $tag) {
    //
}

//或者
$tag = App\Tag::find(1);

foreach ($tag->videos as $video) {
    //
}



//orm查询关联表条件
$result = $this->model->where($where)->whereHas('store', function ($query) use ($search) {
    isset($search['store_name']) && $query->where('title', 'like', '%' . $search['store_name'] . '%');
})->orderBy('id', 'desc')->paginate();

zed
请先登录后发表评论
  • latest comments
  • 总共0条评论