Tom*_*ald 128 php laravel eloquent laravel-4 laravel-5
我有一个Eloquent模型,它有一个相关的模型:
public function option() {
return $this->hasOne('RepairOption', 'repair_item_id');
}
public function setOptionArrayAttribute($values)
{
$this->option->update($values);
}
Run Code Online (Sandbox Code Playgroud)
当我创建模型时,它不一定具有相关模型.当我更新它时,我可能会添加一个选项.
所以我需要检查相关模型是否存在,分别更新或创建它:
$model = RepairItem::find($id);
if (Input::has('option')) {
if (<related_model_exists>) {
$option = new RepairOption(Input::get('option'));
$option->repairItem()->associate($model);
$option->save();
$model->fill(Input::except('option');
} else {
$model->update(Input::all());
}
};
Run Code Online (Sandbox Code Playgroud)
<related_model_exists>我正在寻找的代码在哪里.
Jar*_*zyk 170
在php 7.2+中你不能count在关系对象上使用,所以对于所有关系都没有一个通用的方法.使用查询方法改为@tremby,如下所示:
$model->relation()->exists()
Run Code Online (Sandbox Code Playgroud)
处理所有关系类型的通用解决方案(pre php 7.2):
if (count($model->relation))
{
// exists
}
Run Code Online (Sandbox Code Playgroud)
这将适用于每个关系,因为动态属性返回Model或Collection.两者都实现ArrayAccess.
所以它是这样的:
单一关系: hasOne/belongsTo/ morphTo/morphOne
// no related model
$model->relation; // null
count($model->relation); // 0 evaluates to false
// there is one
$model->relation; // Eloquent Model
count($model->relation); // 1 evaluates to true
Run Code Online (Sandbox Code Playgroud)
对多关系: hasMany/belongsToMany/ morphMany/ morphToMany/morphedByMany
// no related collection
$model->relation; // Collection with 0 items evaluates to true
count($model->relation); // 0 evaluates to false
// there are related models
$model->relation; // Collection with 1 or more items, evaluates to true as well
count($model->relation); // int > 0 that evaluates to true
Run Code Online (Sandbox Code Playgroud)
tre*_*mby 69
甲Relation对象通过未知方法调用通过一个锋查询生成器,其被设置为仅选择相关的对象.该Builder又将未知方法调用传递给其底层查询Builder.
这意味着您可以直接从关系对象使用exists()或count()方法:
$model->relation()->exists(); // bool: true if there is at least one row
$model->relation()->count(); // int: number of related rows
Run Code Online (Sandbox Code Playgroud)
注意以下括号relation:->relation()是函数调用(获取关系对象),而不是->relationLaravel为您设置的魔术属性获取器(获取相关对象/对象).
count在关系对象上使用该方法(即使用括号)将比执行$model->relation->count()或更快count($model->relation)(除非关系已被急切加载),因为它运行计数查询而不是拉动任何相关对象的所有数据从数据库中,只是为了计算它们.同样,使用exists也不需要拉模型数据.
无论exists()和count()工作在我已经尝试了所有关系类型,所以至少belongsTo,hasOne,hasMany,和belongsToMany.
Haf*_*ari 18
我更喜欢使用exists方法:
RepairItem::find($id)->option()->exists()
检查相关模型是否存在.它在Laravel 5.2上运行良好
在Php 7.1之后,接受的答案将不适用于所有类型的关系.
因为根据类型的关系,Eloquent将返回a Collection,a Model或Null.在Php 7.1中 count(null)会抛出一个error.
因此,要检查是否存在关系,您可以使用:
对于单身关系:例如hasOne和belongsTo
if(!is_null($model->relation)) {
....
}
Run Code Online (Sandbox Code Playgroud)
对于多个关系:例如:hasMany和belongsToMany
if ($model->relation->isNotEmpty()) {
....
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我用于单一关系:hasOne,belongsTo和morphs
if($model->relation){
....
}
Run Code Online (Sandbox Code Playgroud)
因为如果条件为空,则这将为假。
对于多重关系:hasMany,belongsToMany和morphs
if ($model->relation->isNotEmpty()) {
....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113919 次 |
| 最近记录: |