I have a small table and try to use some filters to get some specific data out of it.
My attempt:
$matchingCars = Car::where([
['brand', '=', request('brand')],
['model', '=', request('model')],
['price', '<', request('maxPrice')],
])->get();
Result:
Collection {#249 ▼
#items: []
}
You can see nothing is returned. However, if I do the queries one by one and count the result, then I get a number higher than 0, so there are models who pass my filters!
$checkBrands = Car::where('brand', '=', request('brand'))->get();
$checkModels = Car::where('model', '=', request('model'))->get();
$checkPrice = Car::where('price', '<', request('maxPrice'))->get();
echo count($checkBrands) . "<br>"; //output: 1
echo count($checkModels). "<br>"; //output: 1
echo count($checkPrice). "<br>"; //output: 8
die;
Why are they not stored in the collection?
You need to orWhere():-
$matchingCars = Car::where('brand', '=', request('brand'))
->orwhere('model', '=', request('model'))
->orwhere('price', '<', request('maxPrice'))->get()
Note:-
you said:- However, if I do the queries one by one and count the result, then I get a number higher than 0
But this doesn't mean that combination of all these three queries with AND will return result.
So apply OR condition like above.
Try this:
$matchingCars = Car::where('brand', '=', request('brand'))
->where('model', '=', request('model'))
->where('price', '<', request('maxPrice'))->get();
$matchingCars = Car::where('brand', '=', request('brand'))
->where('model', '=', request('model'))
->where('price', '<', request('maxPrice'))->get();
And make sure that that you have the right things returned by request()
Related
Code below:
$data = DB::table('information')
->select(DB::raw('SUM(working) as working'))
->where('name', '=', 'ismael')
->get();
error_log($data);
it print: [{"working":"23"}]
how can I get the 23? without the "working"
$data = DB::table('information')
->select(DB::raw('SUM(working) as working'))
->where('name', '=', 'ismael')
->first()->working;
Use method sum:
DB::table('information')->where('name', '=', 'ismael')->sum('working')
Or call the attribute:
DB::table('information')->where('name', '=', 'ismael')->select(DB::raw('SUM(working) as working'))->first()->working
I have a collection and I want to filter that collection based upon "days_since_last_wallet_transaction" by using an array. For example, I have an array $day = array(2,4,6,8). Now I want to get the record where days_since_last_wallet_transaction is in 2,4,6,8. I think I cannot use "days_since_last_wallet_transaction" in where clause. Here is my query:
Customer::select(
'customers.id',
'customers.wallet_amount',
'customers.onesignal_id',
'customers.phone_number',
'customers.name',
DB::raw('DATEDIFF(NOW(), max(wallet_transactions.created_at)) as days_since_last_wallet_transaction')
)
->join('wallet_transactions', function ($join){
$join->on('customers.id', '=', 'wallet_transactions.customer_id')
->where('wallet_transactions.amount', '>', 0);
})
->groupBy('customers.id')
->where('customers.wallet_amount', '>', 0);
Any help would be highly appreciable.
Considering below your array:
$days = [2,4,6,8];
create a raw query as below (Note: you can write this line in query also )
$raw_query = '(DATEDIFF(NOW(), max(wallet_transactions.created_at))) IN ('.implode(',',$days).')';
Your query
Customer::select(
'customers.id',
'customers.wallet_amount',
'customers.onesignal_id',
'customers.phone_number',
'customers.name',
DB::raw('DATEDIFF(NOW(), max(wallet_transactions.created_at)) as days_since_last_wallet_transaction')
)
->join('wallet_transactions', function ($join){
$join->on('customers.id', '=', 'wallet_transactions.customer_id')
->where('wallet_transactions.amount', '>', 0);
})
->where('customers.wallet_amount', '>', 0)
->havingRaw($raw_query)
->groupBy('customers.id');
I'm trying filtered a collection, but the code in the last row does not work. In this row the $property->county_id is an integer, the params.county_id is an array. I would like know the array contains the integer.
I think the code is wrong, because the key (maybe) must be the params.county_id. How I can do this?
Thanks the answers.
$buyerSearches = collect($items);
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->whereIn($property->county_id, 'params.county_id');
Be sure that the second argument in the whereIn is an array or create that array before the $result query and use it next.
I solved, it works :)
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->filter(function($buyerSearch) use ($property) {
return in_array($property->county_id, $buyerSearch['params']['county_id']);
});
Try this code instead :
->whereIn('county_id', 'params.county_id');
I wondered how to make a Where All clause with Laravel
I'm trying to check if the episodes that a user saw are all the episodes of the series.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$alleps get all the episodes of the serie
$seriessaw get all the episodes a user saw
Thank you for your answers !
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I think you would need to set a having clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes table.
When you use WhereIn, you have to pass an array as the second parameter.
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id)->get()->toArray();
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I have the following query, which does not give me the expected result:
$query = $query->join('events_dates', function($join) use ($data){
$join->on('events.id', '=', 'events_dates.event_id')
->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
->orWhere('recurrent', "=", 1)
->where((strtotime($data["date_start"]) - strtotime('event_dates.start_date')) % ('events_dates.repeat_interval' * 86400), '=', 0);
});
There are 4 where clauses in this query.
The requirement is that either the two first where clauses are executed, or either two last depending on the recurrentfield.
PHP returns an error division by zero, because the last Where clause should not be executed when recurrentis 0.
Any suggestions?
I don't know your exact goal nor do I know what your db looks so this is just a wild guess:
$query = $query->join('events_dates', function($join) use ($data){
$join->on('events.id', '=', 'events_dates.event_id')
->where('events_dates.start_date', "<=", date_format(date_create($data['date_end']), "Y-m-d"))
->where('events_dates.end_date', '>=', date_format(date_create($data['date_start']), "Y-m-d"))
->orWhere('recurrent', "=", 1)
->whereRaw('DATEDIFF(?, event_dates.start_date) % event_dates.repeat_interval = 0', array($data['date_start']));
Update
This might help for the between two dates part
->where('events_dates.start_date', '<=', new Carbon($data['date_end']))
->where('events_dates.end_date', '>=', new Carbon($data['date_start']))