我该如何在laravel中建立条件?

问题描述 投票:0回答:1

我有File模型来存储我的图像,而我有shop模型,这家商店可以有很多图像,在商店模型中,我编写了此代码:

public function Files()
    {
        return $this->hasMany('system\models\file', 'attachment_id');
    }

并且我想用它的图像来检索商店模型,现在我的问题是如何将条件置于相关位置。我用于检索商店模型的代码是:

$shop = Shop::where('id', $id)->with('ShopType', 'User', 'Files')->get();

note:我需要为关系设置两个条件,即在Files方法中调用attachment_id的主题集之一,第二个条件是attachment_type请帮我。谢谢

laravel model relation data-retrieval wherehas
1个回答
0
投票

我有这个为我工作

// USER model
public function orders()
    {
        return $this->hasMany(Order::class, 'user_id');
    }

// Controller
user->orders()->where('id', $id);
© www.soinside.com 2019 - 2024. All rights reserved.