Why Laravel gives wrong result query? - laravel

$groups = Groups::where("min", '>=', $result->Z)
->where("max", '<=', $result->Z)
->orderBy('min')
->get();
Where $result->Z is 52.850294770880225.
So, I should get row:
4 | 47.01 | 52.99 | 0
Instead empty:
Collection {#631 ▼
#items: []
}

You have your conditions the wrong way round. You're currently saying where min is bigger or equal to the value and max is smaller or equal to the value (which in theory should never happen).
Try:
$groups = Groups::where("min", '<=', $result->Z)
->where("max", '>=', $result->Z)
->orderBy('min')
->get();
Notice I've swapped the >= and <= around.

Related

Finding the exact occurrence of Laravel

Products::whereIn('category_id', ['223', '15', '20'])
->where('active', 1)
->get();
How can I fix this example so that it finds the exact occurrence of category_id = 223 and 15 and 20 and also necessarily active = 1?
It makes no sense to look for exactly category_id = 15, 20 and 223, it can only be one...
But, if you still want that, your query is nearly there, you can do:
Products::where('category_id', '223')
->where('category_id', '15')
->where('category_id', '20')
->where('active', 1)
->get();
But, again, that should return an empty Collection. whereIn is a simple IN in SQL, that means it is an OR for all the values you want.
Use from this group. Because, maybe you use OR condition in future. Therefore, your condition will not work correctly.
For example
Products::query()->where(function ($query){
$query->where('category_id', '223')
->where('category_id', '15')
->where('category_id', '20');
})->where('active', 1)
->get();
Explain of query above will work like that:
Select * from products where (category_id = 223 and category_id = 15 and category_id = 20) and active = 1

I want to find the value who exist in array

I have 2 queries and I would like to know the elements (date_debut) of the second query which exists in the first query (dateincal). The elements of the second query can appear one or more times in the first query.
once the dates (date debut) (dateincal) have been found i want to be able to then retrieve also the other information for the element found
$feries = Jourferie::Select('dateincal', 'description')
->where('dateincal', '>=', Carbon::now()->startOfMonth())
->where('dateincal', '<=', Carbon::now()->endOfMonth())
->get();
$plannings = Planning::Select('date_debut', 'id', 'agent_id', 'site_id')
->where('id', '!=', 0)
->where('statut', 'provisoire')
->get();
I don't know if that way can help you:
foreach($feries as $ferie){
$myresult = $plannings->contains('date_debut',$ferie->dateincal)
/* do things */
}

Laravel eloquent Int greater then column but lest then other column

I have a weird problem and can't understand why it happens.
I have a table where row has start = 300 and end = 400.
When I try to filter:
$price = 320;
Price::where('start', '>=', $price)->where('end', '<=', $price)->first()
And I have always an empty result. All columns set to an integer. '$price' is an integer. Why I get an empty result, have no idea...
Your query fails because you're saying get price where the start is greater than or equal to 320 (yours is 300 so fails here) AND where end is less than or equal to 320 (yours is 400 so technically this passes)
but since you are doing an AND query the whole query returns nothing.
Try this
Price::where('start', '>=', $price)->orWhere('end', '<=', $price)->first();
or this
Price::where('start', '<=', $price)->where('end', '>=', $price)->first();

Counting columns with if conidition in query

Counting only columns in which, Buy_Rate is more than Sell_Rate.
My query is not resulting as per expected, it is resulting me wrong.
$user_id = Auth::user()->id;
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)->where('buy_rate', '<', 'sell_rate')->get()->count();
Inverse: If sell_rate is more than buy_rate then only, count the columns.
You can use whereColumn
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)
->whereColumn('buy_rate', '<', 'sell_rate')
->count();
Also there is no need to call get() when you need count() from query builder
laravel eloquent where don't support comparing columns. so you need use raw SQL in order to compair two columns. you can do something like,
$user_id = Auth::user()->id;
$losing_trades_count = FinalTrade::where('user_id', '=', $user_id)->whereRaw('buy_rate < sell_rate')->get()->count();
hope this helps!

Laravel search for the close results

My app has an option to battle another player.
In order to match a player with another, I created a database field named score in my users table.
My goal is to match user with a another random user who is already SEARCHING for a battle. Both of the users has to be on +-100 score difference.
For example, user with score of 100 can battle players with score of 0-200.
I'm trying to search for all of the available options, but couldn't get how to do this.
Currently, I just output the first one in SEARCHING mode.
$pvp = PvpBattle::where('mode','=','SEARCHING')
->first();
How I can get all PvpBattle objects where the associated player1_id has +- 100 score difference from $this->me->score?
Try This code
WhereHas('users') is the user relation between PvpBattle and user
$minScore = $this->me->score - 100;
$maxScore = $this->me->score + 100;
$pvp = PvpBattle::where('mode','=','SEARCHING')
->WhereHas('users', function ($query) use ($minScore, $maxScore){
$query->Where(function ($subQuery) use ($minScore, $maxScore){
$subQuery->orWhere('score', '<=', $maxScore)
->orWhere('score', '>=', $minScore);
});
})
->first();
First find the score of player one to $scoreA or use $this->me->score
Then
$opp_list=PvpBattle::where('mode','=','SEARCHING')->Where(function($query)
{
$query->where('score', '>=', $scoreA -100)
->orwhere('score', '<=', $scoreA+100);
})
->get();
This gets all the players in mode searching with condition score +-100. first() returns the first result from the table.
How are you going to handle two requests at the same time pointing to the same user?
User A should be not returned as a result.
Add ->where('user_id', '!=', $user_id) to query
Use a join with the users table to filter the result by the score of the related user.
$pvps = PvpBattle::join('users', 'users.id', '=', 'pvpbattle.user_id')
->where('pvpbattle.mode','=','SEARCHING')
->where('users.score', '>=', $this->me->score - 100)
->where('users.score', '<=', $this->me->score + 100)
->get()
;

Resources