'how to use whereHas in laravel

I am new to laravel, I need to create a query for the db,

 $query = Deal::query();

I want to use the wherehas operator. this is my code that is worked.

else if ($group_by == 'precedence') {
                if($get_deals_for == 'noprecedence'){
                    $get_deals_for = 0;
                }
                $precedenceStatus = $get_deals_for;
                $query-> where('precedence', '=', $precedenceStatus);
            //    \Log::info('The request precedence: '.$precedenceStatus);
               

            }

I want to add this code also to the query

if($person) {
                    $query->whereHas('personnel', function ($subQuery) use ($person) {
                        $subQuery->where('id', '=', $person);
                    });
                }

So I need to change the first code? how I can convert the first code to wherehas? the first code is from table called deal, the second section is from realtionship called personnel. the second section worked in other places in the code, I just need to fix the first section and not understand what to write in the use

I try this and get error on the last }

else if ($group_by == 'precedence') {
                if($get_deals_for == 'noprecedence'){
                    $get_deals_for = 0;
                }
                $precedenceStatus = $get_deals_for;
                $query-> where('precedence', '=', $precedenceStatus)
                      -> when ($person, function($query) use($person) {
                        $query->whereHas('personnel', function ($query) use ($person) {
                            $query->where('id', '=', $person);
                        });
                    })
                }


Solution 1:[1]

There is a method you can use called when(, so that you can have cleaner code. The first parameter if true will execute your conditional statement.

https://laravel.com/docs/9.x/queries#conditional-clauses

$result = $query
    ->where('precedence', '=', $precedenceStatus)
    ->when($person, function ($query) use ($person) {
        $query->whereHas('personnel', fn ($q) => $q->where('id', '=', $person));
    })
    ->get();

You should also be able to clean up your precedence code prior to that using when( to make the entire thing a bit cleaner.

Solution 2:[2]

Querying to DB is so easy in laravel you just need to what you want what query you want execute after that you just have to replace it with laravel helpers.Or you can write the raw query if you cant understand which function to use. using,DB::raw('write your sql query').

Now Most of the time whereHad is used to filter the data of the particular model. Prefer this link,[Laravel official doc for queries][1] like if you have 1 to m relation ship so u can retrive many object from one part or one part from many object.like i want to filter many comments done by a user,then i will right like this.

$comments = Comment::whereHas('user', function (Builder $query) {
$query->where('content', 'like', 'title%');

})->get();

$comments = Here will be the model which you want to retrive::whereHas('relationship name', function (Builder $query) { $query->where('content', 'like', 'title%'); })->get();

you can also write whereHas inside whereHas. [1]: https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 miken32
Solution 2 VANITA CHAUDHARI