Laravel Enloquent - 如果列值为NULL或0,请勿运行关系查询 [英] Laravel Eloquent - Do not run relationship query if column value is NULL or 0

查看:731
本文介绍了Laravel Enloquent - 如果列值为NULL或0,请勿运行关系查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的口述中有各种各样的关系 模型,如下所示:

  public function main_image()
{
return $ this-> hasOne(Media :: class,'id','main_image_id' );
}

但是,如果 main_image_id 为null或0,所以我最终得到如下这样的查询:

  select * from`media`,`media`.`id`是null,`media`.`id`不是null限制1 

这显然不会返回任何东西,但仍然浪费资源。有没有办法自动检查的?



目前我所做的是一个方法,如 hasMainImage(),它检查 main_image_id 不是null而不是0,但是很多当前系统已经使用了关系,我想知道是否可以将检查添加到关系方法本身?



如果列没有实际值,我已经尝试添加一个支票并返回 null ,但是我有一个异常,它必须返回一个Relation对象。或者如果我试图加载它,我收到以下错误:



调用成员函数addEagerConstraints()on null

  public function main_image()
{
if(!$ this-> main_image_id){
return null;
}

返回$ this-> hasOne('App \Modules\Media\Models\Media','id','main_image_id');
}

感谢您的帮助!



$ p






= ;
var_dump($ page-> main_image); //这将运行一个如上所示的查询,保证不返回
//因为在这一点上系统知道$ page-> main_image_id为0或为null,我想使用它来不运行查询并自动将$ page-> main_image设置为null


解决方案

您以错误的方式申报关系,雄辩文档是清楚这一点。



本身没有意义的实体(没有父可能会说)应该包含belongsTo关系(如文档所示)



对于演示,您可以说您拥有用户模型,并且您有许多关于该用户的详细信息,以便创建详细信息模型。



现在用户模型应该有详细的关系:

  public function detail(){
return $ this-> ; hasOne('App \Detail','use r_id','id'); //您可以从代码示例中看到差异,您有第二和第三参数反转
}

细节模型应该有用户关系:

  public function user()
{
return $ this-> belongsTo('App\User');
}


I have various relationships within my Eloquent Models that look like this:

public function main_image()
{
    return $this->hasOne(Media::class, 'id', 'main_image_id');
}

However, it will run a SQL query if the main_image_id is null or 0, so I end up with a number of queries like this:

select * from `media` where `media`.`id` is null and `media`.`id` is not null limit 1

Which obviously will not return anything, but still wastes resources. Is there any way of automatically checking for that?

Currently what I do is have a method, like hasMainImage(), that checks that main_image_id is not null and not 0, but a lot of the current system already uses the relationships, and I was wondering if I can add the check to the relationship method itself?

I have tried adding a check to it and return null if the column has no real value, but I've got an Exception that it has to return a Relation object. Or if I'm trying to Eager load it, I receive following error:

Call to a member function addEagerConstraints() on null

public function main_image()
{
    if (!$this->main_image_id) {
        return null;
    }

    return $this->hasOne('App\Modules\Media\Models\Media', 'id', 'main_image_id');
}

Thanks for your help!

EDIT: A perhaps more clear example:

$page = Page::find(1);
var_dump($page->main_image); // This will run a query as shown above that is guaranteed to return nothing
// Since at this point system knows that $page->main_image_id is 0 or null, I would like to use that to not run the query and automatically set $page->main_image to null

解决方案

You declare your relations in wrong order, Eloquent documentation is clear about this.

The entity that does no make sense by itself (without its "parent" you may say) should incorporate "belongsTo" relation (as documentation says inverse).

For demonstration lets say you have User model and you have many details about that user so you create Detail model.

Now User model should have detail relation:

public function detail() {
    return $this->hasOne('App\Detail', 'user_id', 'id'); //you can see the difference here from your code sample, you have 2nd and 3rd parameter reversed
}

And Detail model should have user relation:

public function user()
{
    return $this->belongsTo('App\User');
}

这篇关于Laravel Enloquent - 如果列值为NULL或0,请勿运行关系查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆