Pic for table in blade:
and
See this pic for table in database:
In my advanced search I have 2 input box which is round 1 and round 2.
If I search for 1 in round 1 and also 1 in round 2 it should display only the data of id 1.
It's like:
$query->where(['column.round' => 1, 'column.score' => $request->score1] and ['column.round' => 2, 'column.score' => $request->score2]);
My question is how to have and operator with this to show only the data of id 1.
This is like multiple rows with the same id in 1 column.
if i get you right u only want one RAW back instead of a collection
if that is the case then:
$query->where(['round1' => 1, 'round2' => 1])->first();
Can you try this code. Refer here
$query->where(function ($roundQuery) {
$roundQuery->where('round', '=', 1)
->orWhere('round', '=', 2);
})
->where(function ($scoreQuery) use($request) {
$scoreQuery->where('score', '=', $request->score1)
->orWhere('score', '=',$request->score2);
})
->get();
Related
I want to validate the is_default column based on domain_id. The condition is for every domain_id there can be only one(single) 1; others multiple will be 0 (zero).
I try to make a rule in Laravel that's like the following. But It's not working; I know it's wrong. So what would be the best query?
$query = Restaurant::where($attribute, $value)
->where('domain_user.domain_id', request()->get('domain_id'));
Here is something you might want:
function updateDefault($domain_id, $name) {
//updating default domain with given name
Restoraunt::where('domain_id',$domain_id)->where('name', $name)->first()->update(['is_default' => 1]);
//setting all other models with same domain_id to 0
Restoraunt::where('domain_id', $domain_id)->where('name', '!=', $name)->update(['is_default' => 0]);
}
If you for example use updateDefault(1, 'ABC - 2') results would be :
I'm trying to do a simple query using Eloquent. My test_registrants table looks like this
I want to add new column with value of all user_id with payment_status = 1
This is my query using whereColumn
TestRegistrant::select(['test_registrants.*'])
->where('payment_status', 1)
->addSelect([
'attempt' => TestRegistrant::select(DB::raw('count(*) as attempt'))
->whereColumn('test_registrants.user_id', 'user_id')
->where(function ($query) {
$query->where('payment_status', 1);
})
]);
but I get all user_id instead
What I'm trying to achieve is this one
So what do I do wrong here? thank you
The reason your query is returning 3, is because it is simply counting all the records that have payment_status = 1. The whereColumn() is not working properly, because it does not reflect the right columns.
When you define an alias for the user_id column on the test_registrants table, it should work. For example, you could name it: outer_user_id. I have updated your example accordingly:
TestRegistrant::select(['test_registrants.payment_status', 'test_registrants.user_id as outer_user_id'])
->where('payment_status', 1)
->addSelect([
'attempt' => TestRegistrant::selectRaw('count(*) as attempt')
->whereColumn('test_registrants.user_id', 'outer_user_id')
->where(function ($query) {
$query->where('payment_status', 1);
})
])
->get();
Alternatively, you could also look into grouping the results, so that you can count all the rows in a specific group.
I have an eloquent query that gets the total count records (created_at) of the last 7 days. But the problem is if one of these days have 0 records, this doesn't appear in the final data.
My query:
$data = Data::whereBetween('created_at', [Carbon::now()->subDays(6)->format('Y-m-d')." 00:00:00", Carbon::now()->format('Y-m-d')." 23:59:59"])
->groupBy('date')
->orderBy('date')
->get([
DB::raw('DATE(created_at) as date'),
DB::raw('count(*) as total')
])
->pluck('total', 'date')->toArray();
What I get:
[
"2020-04-14" => 1
"2020-04-16" => 1
"2020-04-18" => 1
"2020-04-19" => 1
]
What I expected:
[
"2020-04-14" => 1
"2020-04-15" => 0
"2020-04-16" => 1
"2020-04-17" => 0
"2020-04-18" => 1
"2020-04-19" => 1
"2020-04-20" => 0
]
Any suggestions?
SOLUTION:
-Based on Gary Houbre's proposal:
$results = Data::whereBetween('created_at', [Carbon::now()->subDays(6)->format('Y-m-d')." 00:00:00", Carbon::now()->format('Y-m-d')." 23:59:59"])
->groupBy('date')
->orderBy('date')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as date'),
DB::raw('count(*) as total')
])
->keyBy('date')
->map(function ($item) {
$item->date = Carbon::parse($item->date);
return $item;
});
$period = new DatePeriod(Carbon::now()->subDays(6), CarbonInterval::day(), Carbon::now()->addDay());
$graph = array_map(function ($datePeriod) use ($results) {
$date = $datePeriod->format('Y-m-d');
return $results->has($date) ? $results->get($date)->total : 0;
}, iterator_to_array($period));
Looking directly Sql : How to include "zero" / "0" results in COUNT aggregate?
Into a same table : How to get the record if Count is zero in Laravel
You need to add an outer join into your request with Eloquent.
My idea is to create a for loop to check the days.
If there is no record on a date then print 0
Loop Iteration:
Catch the first Day (Suppose 14)
Catch the last Day
Then check in every iteration it is greater than one or many
Thus, I hope you will get normally.
We had a similar problem while trying to put back-end data into the chart. Since some of the days were missing it didn't look well. Our solution was;
Create a function like this;
public function generateDates(Date $startDate, Date $endDate, $format = 'Y/m/d'): Collection
{
$dates = collect();
$startDate = $startDate->copy();
for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
$dates->put($date->format($format), 0);
}
return $dates;
}
In your case it's going to be (today and today - six days) and you will union returning collection with your query collection. What it does is; it create a date range from the keys and fill them with zero. When your query collection has some value other than zero - it is going to overwrite it.
Im trying to design a query, but I have no idea where to start.
I'll type it out how I want it to function.
Items::whereIn('id',$ids)->orWhereIn('id_2',$ids)->where('type','!=',$type)->get();
Thats how I want it to work, but I know that wont work, because it will just ignore the WHERE type=$type query, because the whereIN's would have already pulled records, that dont adhere to the Where query.
Basically I want the eloquent version of this...
"SELECT * FROM items WHERE type!=$type AND (id IN (1,2,3) OR id_2 IN(1,2,3))"
What you are attempting to do is to group statements: https://laravel.com/docs/5.3/queries#parameter-grouping
What you need to do is to make the code something like this:
Items::where('type', '!=', $type)
->where(function ($query) use ($ids) {
$query->whereIn('id',$ids)
->orWhereIn('id_2',$ids);
})
->get();
That way you are grouping the where in clauses.
$user_id = 2 ;
$user_ids = [2,3,4,5,6,7,78,87,88,90] ;
where('id',$user_id) vs whereIn('id',$user_ids)
Note: where will compare with just first value of array or just one single value. and whereIn will compare evey index of array.
You're gonna want to do something like this:
Item::where('type','!=','ATypeTest')->where(function ($query) {
$query->whereIn('id',[1, 2, 3])->orWhereIn('id_2',[1, 2, 3]);
})->get();
Check the Laravel documentation on more regarding grouping: https://laravel.com/docs/5.3/queries#parameter-grouping
This will by the way generate the following SQL query:
SELECT * FROM "items" WHERE "type" != `ATypeTest` AND ("id" in (1, 2, 3) OR "id_2" in (1, 2, 3))
For some reason I don't get the expected result when doing the following:
$cars = Car::query();
$cars->whereIsVisible(true);
# output: $list
// [2016-01-16 09:30:04] local.INFO: array (
// 0 => 5,
// 1 => 7,
// 2 => 9,
// 3 => 3,
// )
$cars->whereHas('specifications', function($query) use ($list) {
$query->whereIn('id', ($list));
});
$cars->get();
What I expect is that I get only cars that have all the specifications that are inside that $list, but that's not correct. Even when I fill in more specifications, I get a bigger result. So there goes something wrong.
I'm used to Eloquent, so I suck in queries. But this is the Query:
select * from "cars" where "cars"."deleted_at" is null
and "is_visible" = true
and (select count(*)
from "specs" inner join "car_specs"
on "specs"."id" = "car_specs"."facility_id"
where "car_specs"."car_id" = "cars"."id"
and "id" in (5, 7, 9 ,3)) >= 1
Anyone see where it goes wrong? And how to fix it?
According to https://laravel.com/docs/5.1/eloquent-relationships#querying-relations
whereHas just checks that you have cars with the provided specifications, not that it returns those cars that match your specifications.
I think you should use where directly.