laravel - Laravel Eloquent 返回值取决于关系的值
问题描述
我正在尝试根据列的值从表中返回数据。让我解释。
我有 3 张桌子。books
,genres
和一个数据透视表来关联这两个表的值。
表
books
genres
book_genre
存在many-to-many
关系,因为 abook
可以包含 differentgenres
并且 agenre
可以与 different 相关联books
。
关系
Book Model
class Book extends Model
{
use HasFactory;
/**
* @inheritdoc
*
* @var string[]
*/
protected $fillable = [
'title',
'language',
'thumbnail',
'active'
];
/**
* @inheritdoc
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new ExcludeBookWhenInactiveRelations());
}
/**
* Genders relationship
*
* @return BelongsToMany
*/
public function genres()
{
return $this->belongsToMany(Genre::class);
}
}
Genre Model
class Genre extends Model
{
use HasFactory;
/**
* @inheritdoc
*
* @var string[]
*/
protected $fillable = [
'name',
'active'
];
/**
* Books relationship
*
* @return BelongsToMany
*/
public function books()
{
return $this->belongsToMany(Book::class);
}
}
现在,我正在尝试global scope
在我的Book
模型中创建一个,因此在查询时,它只返回未被禁用books
的。genres
也就是说,如果在我的genre
表中,我将active
a 的属性更改genre
为 false 并且genre
与 a 相关联book
,那么它不应该向我显示book
.
这是Global Scope
我正在创建的:
class ExcludeBookWhenInactiveRelations implements Scope
{
/**
* @inheritdoc
*
* @param Builder $builder
* @param Model $model
* @return Builder|void
*/
public function apply(Builder $builder, Model $model)
{
return $builder->whereHas('genres', function ($query) {
return $query->where('active', true);
});
}
}
发生的情况是,当我将 a 的active
属性值更改为genre
时false
,它不会显示genere
在集合中,但如果它book
有 2genres
并且其中一个将active
属性保留在 中true
,那么它会跟随我返回book
,以及我'我想做的是如果book
至少有 1 个gender
disabled (false)
,那么它不会在集合中显示它。
你能帮我解决我的问题吗?非常感谢您提前。
解决方案
您雄辩的设置正在创建此查询。
select * from books where exists (select * from genres inner join book_genre on genres.id = book_genre.genre_id where books.id = book_genre.book_id and active = 1)
在这里,如果书只有一种活跃的流派,它将被选中。这是错误的。现在您需要另一个没有太大区别的查询。
select * from books where not exists (select * from genres inner join book_genre on genres.id = book_genre.genre_id where books.id = book_genre.book_id and active = 0)
在这里,如果书籍只有一种不活跃的流派,它将不会从最终记录中被选中。
@leo95batista 雄辩地提供了这个查询。
class ExcludeBookWhenInactiveRelation implements Scope
{
/**
* @inheritdoc
*
* @param Builder $builder
* @param Model $model
* @return Builder|void
*/
public function apply(Builder $builder, Model $model)
{
return $builder->whereDoesntHave('genres', function (Builder $query) {
return $query->where('active', false);
});
}
}
推荐阅读
- codenameone - 无法在 iOS 上获取 deviceId
- javascript - 如何获取焦点文本框的 id?
- python - 如何使用 Python 在检查->网络中获取 XHR URL?
- angular - 如何在使用单向绑定更改元素后访问更新的元素
- activemq - 很好地使用桥接 MQTT 代理
- excel - Excel公式仅查找可见单元格的条件最小值
- oracle - Spring Boot - 重新启动后重新连接到数据库
- javascript - 在 Edge 和 IE 上修补 document.write
- python - Python 3:在地图上绘制 GPS 位置
- python - 将列表转换为列表列表