PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to check relation data of a collection before sending it to view

 April 19, 2022     laravel, laravel-5.8, php     No comments   

Issue

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?


Solution

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



Answered By - lagbox
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing