如何在发送查看之前检查集合的关系数据

发布于 2025-01-20 02:22:05 字数 885 浏览 4 评论 0 原文

我有一个像这样的控制器方法:

public function awaiting()
    {
        $producers = Producer::where('producer_process',4)->get();
        $producers_list = [];
        foreach($producers as $producer){
            if($producer->brand->brand_rejected == 0){
                array_push($producers_list, $producer);
            }
        }

        return view('admin.brands.awaiting', compact('producers_list'));
    }

所以基本上在Producer模型和模型之间存在一对一关系。 品牌型号。

为了获取 makers_process4 以及相关的 brand_rejected 字段的 brands 表记录的集合brands 表记录必须设置为0,我添加了一个array_push 并检查条件。

现在这工作正常并且正确显示了正确的数据,但我想知道,使用 Eloquent 关系执行此操作的简写方法是什么?

我的意思是,是否有任何用 Eloquent 关系编写的简洁且有用的方法可以在不使用 array_push 或另一个 foreach 循环的情况下完成此操作?

I have a Controller method like this:

public function awaiting()
    {
        $producers = Producer::where('producer_process',4)->get();
        $producers_list = [];
        foreach($producers as $producer){
            if($producer->brand->brand_rejected == 0){
                array_push($producers_list, $producer);
            }
        }

        return view('admin.brands.awaiting', compact('producers_list'));
    }

So basically there's One To One relationship between Producer model & Brand model.

In order to get the collection of brands table records that has producer_process of 4 and ALSO the brand_rejected field of related brands table record must be set to 0, I added an array_push and check the condition.

Now this works fine and properly shows the correct data but I wanted to know, what is the shorthand method of doing this with Eloquent relationships?

I mean is there any concise and useful method written in Eloquent relationships that can do this without using array_push or another foreach loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2025-01-27 02:22:05

您可以使用 whereHas 根据关系的存在来约束结果集。在这里,我们说我们只希望生产商的“product_process”字段设置为 4,并且其品牌的“brand_rejected”字段设置为 0:

$producers = Producer::where('producer_process', 4)
    ->whereHas('brand', function ($q) { $q->where('brand_rejected', 0); })
    ->get();

如果您希望这些生产商加载其品牌关系以供使用,您应该立即加载那。在 get 调用之前,您可以告诉它加载关系:

$producers = Producer::where(...)->whereHas(...)->with('brand')->get();

Laravel 5.8 文档 - Eloquent - 关系 - 查询关系存在 whereHas

Laravel 5.8 文档 - 雄辩 - 关系 - 热切加载 with

You can use whereHas to constrain the result set based on the existence of a relationship. Here we are saying we only want producers that have the field 'produce_process' set to 4 and have a brand with a field of 'brand_rejected' set to 0:

$producers = Producer::where('producer_process', 4)
    ->whereHas('brand', function ($q) { $q->where('brand_rejected', 0); })
    ->get();

If you want these producers to have their brand relationship loaded to use you should eager load that. Before the get call you can tell it to load the relationship:

$producers = Producer::where(...)->whereHas(...)->with('brand')->get();

Laravel 5.8 Docs - Eloquent - Relationships - Querying Relationship Existence whereHas

Laravel 5.8 Docs - Eloquent - Relationships - Eager Loading with

川水往事 2025-01-27 02:22:05

你可以试试这个:

public function awaiting()
{
    $producers = Producer::where('producer_process',4)
      ->with('brand', function($q) {
          $q->where('brand_rejected', 0);
    })->get();

    // dd($producers);
    dd($producers->pluck(brand));

You can try this:

public function awaiting()
{
    $producers = Producer::where('producer_process',4)
      ->with('brand', function($q) {
          $q->where('brand_rejected', 0);
    })->get();

    // dd($producers);
    dd($producers->pluck(brand));
今天小雨转甜 2025-01-27 02:22:05

当然,您可以将方法与()一起使用其中()条款可以应用于关系

示例

$yourQuery->with('brand', function($query){
  $query->where('brand_rejected', 0);
});

的某些条件,请检查此信息

我希望这很有帮助

Sure you can use the method with() also with the where() clause to can apply some conditions to the relationship

Example

$yourQuery->with('brand', function($query){
  $query->where('brand_rejected', 0);
});

check this for more info

https://laravel.com/docs/9.x/eloquent-relationships#constraining-eager-loads

I hope it's helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文