The structure of the table in the database is as follows:
id
body
user_id
group_id
1
test1
1
1
2
test1
2
1
3
test1
3
1
3
test2
4
2
4
test3
5
3
I want to get the first row from each group_id with Eloquent/DB Facade or MongoDB Query in Laravel with paginate:
For Example:
id
body
user_id
group_id
1
test1
1
1
3
test2
4
2
4
test3
5
3
How is this achievable?
for Mongodb use this aggregation
[
{
$sort:{
group_id: 1,
user_id:1
}
},
{
'$group': {
'_id': {
'group_id': '$group_id'
},
'a': {
'$first': '$$ROOT'
}
}
}
]
Use Eloquent's GroupBy as below, it will fetch the first record from the same group:
DB::table('tablename')
->groupBy('group_id')
->get();
Related
I have a model named Student and this model contains some field
id
teacher_id
roll_number
1
1
1
2
1
2
3
1
3
4
1
4
5
1
5
6
1
6
7
1
7
8
1
8
9
1
9
10
1
10
11
2
11
12
2
12
13
2
13
I have a form that passes values teacher_id, start_roll and end_roll
I have a query where I use whereBetween method to get student information
$studentData = Student::where('teacher_id',$request->teacher_id)->whereBetween('roll_number',[$request->start_roll,$request->end_roll])->get();
where start_roll value is 1 and end_roll value is 10 and teacher_id value is 1 but it is strange it only return two array values where id 1 and 10 only
Again if I search start_roll value is 5 and end_roll value is 10 and teacher_id value is 1 but it also strange it returns an empty array
If I print the above query when search it prints like below
Student::where('teacher_id',1)->whereBetween('roll_number',[5,10])->get();
If I execute the above query hardcoded(entry roll_number manually from 5 to 10) then it works fine for me and it returns 6 number of the array where id start 5 and end 10
I don't know why my search is not working. Any suggestion is appreciate please
You should add all requested values 1,2,3,4,5,6,7,8,9,10.
Like This:
$queryRolls = range($request->start_roll,$request->end_roll);
$studentData = Student::where('teacher_id',$request->teacher_id)->whereIn('roll_number',$queryRolls)->get();
I have a query which has needs to order by additional data on relationship columns. This is my most recent attempt:
$odds = Odd::with([
'box', 'set'
])->get()->sortBy(function($query) {
return $query->box->position;
})->sortBy('created_at');
The current database tables are like so:
Odds
id. box_id. created_at
1 1 2021-04-02 17:15:04
2 2 2021-04-02 17:15:04
3 3 2021-04-02 17:15:04
4 4 2021-04-02 17:20:05
Box
id set_id position
1 1 3
2 1 1
3 1 5
4 2 2
The query above is able to sort on odds.created_at, but will not sort on the box.position. The result needs to be in date order then with in date group needs to be ordered by position, like so:
id. box_id. created_at set_id position
2 2 2021-04-02 17:15:04 1 1
1 1 2021-04-02 17:15:04 1 3
3 3 2021-04-02 17:15:04 1 5
4 4 2021-04-02 17:20:05 2 2
I have also tried these methods:
$odds = Odd::with([
'box', 'set'
])->get()->sortBy('created_at')->sortBy('box.position');
$odds = Odd::with([
'box' => function ($query) {
$query->orderBy('position');
},
'set'
])->orderBy('created_at')->get();
I have 2 table - parents and children. I want to return all parents sorted by their children's names. But since parents can have multiple children, I can't seem to figure out how to sort that. I would need to take the child that has alphabetically the first name (of each parent) and use it for sorting somehow.
Basically I would need to do these things
Sort all the children of all parents and get the first child (alphabetically) of each.
For example for parent with id: 9 his child would be Alex Briggs (select * from children where parent_id = 9 order by asc LIMIT 1)
1. Alex Briggs (first one)
------------------------------
2. Ashley Briggs
3. Lucy Briggs
4. Lukas Briggs
And for parent with id: 3
1. Alex Skull (first one)
------------------------------
2. Don Skull
3. Erica Skull
And then return all parents sorted by their first child (alphabetically).
select * from parents order by ( their first child by alphabet .... ASC )
parents table
| id | fullname |
1 Mark Dever
2 John Witney
3 Joey Skull
4 Abraham Lincon
5 Donald Trump
6 Britney Huston
7 Martin Lu
8 Eric Tada
9 Andy Briggs
10 Linda Briggs
children table
| id | parent_id | fullname |
1 9 Lukas Briggs
2 9 Alex Briggs
3 10 Ashley Briggs
4 10 Lucy Briggs
5 1 Mark Driscoll
6 2 Zack Witney
7 2 Victoria Witney
8 3 Alex Skull
9 3 Don Skull
10 3 Erica Skull
result would be like this
{
parent_id: 9,
fullname: Andy Briggs,
children: [
...Alex, Ashley, Lucy...
]
},
{
parent_id: 10,
fullname: Linda Briggs,
children: [
...Alex, Ashley, Lucy...
]
},
{
parent_id: 3,
fullname: Joey Skull,
children: [
...Alex, Don, Erica...
]
},
{
parent_id: 1,
fullname: Mark Dever,
children: [
...Mark...
]
},
{
parent_id: 2,
fullname: John Witney,
children: [
...Victoria, Zack...
]
},
Since you want to order the children name,you can use the below query.
SELECT b.parent_id,a.fullname, GROUP_CONCAT(b.fullname ORDER BY b.fullname ASC) children
FROM parent a
JOIN children b
ON a.id=b.parent_id
GROUP BY b.parent_id,a.fullname;
Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation
I have a table ipd_charges with columns
table - ipd_charges
id doctor room_category charges_cash charges_cashless
1 1 1 200 300
2 1 2 300 400
table - patient_admission
id patient_name tpa_name(if not null, equivalent to charges_cashless)
1 1 Null
2 2 1
table daily_ward_entry
id patient_name room_name doctor_name charges ( from ipd charges)
1 1 1 1 200
2 2 2 1 400
I am trying to use this query which is failing:
$model = \app\models\IpdCharges::find()
->where(['doctor'=>$id])
->andwhere(['room_category'=>$this->room_name])->one();
Thanks. Please tell me if any more info is needed.
A help will be greatly appreciated.
Please check below code :
$ipdCharges = new IpdCharges();
$ipdCharges->findOne(['doctor' => $id, 'room_category' => $this->room_name]);