I am trying to group retrieved rows from database using by months laravel but all the rows are grouped on the the first month.
$data = Medicals::join("patient_referral", "medicals.trx_id", "=", "patient_referral.trx_id")
->where(function ($q) use ($startdate, $enddate) {
$q->whereBetween('medicals.created_at', [$startdate, $enddate])->get();
})
->leftJoin("patients", "medicals.patients_id", "=", "patients.id")
->join("tests", "medicals.tests_id", "=", "tests.id")
->get()
->groupBy(function (Medicals $item) {
return Carbon::parse($item->creted_at)->format('m');;
});
Output
Database (Medicals table)
Please help!!!
You are joining multiple tables. By default, all fields/columns from those tables will be included. We don't really know what created_at field from which table is used.
Medicals::join("patient_referral", "medicals.trx_id", "=", "patient_referral.trx_id")
->where(function ($q) use ($startdate, $enddate) {
$q->whereBetween('medicals.created_at', [$startdate, $enddate])->get();
})
// I select all fields from medicals table. You might want to update this later.
->selectRaw('medicals.*')
->leftJoin("patients", "medicals.patients_id", "=", "patients.id")
->join("tests", "medicals.tests_id", "=", "tests.id")
->get()
->groupBy(function (Medicals $item) {
return Carbon::parse($item->created_at)->format('m');;
});
Related
{owner: '456123', holders: Array(2)}
holders:
0: { equipments: 'test', release_by: 'test'}
1: {equipments: 'test', release_by: 'test'}
owner: "456123"
}
i have this kind of array values object from my react formik app and i want to save it into the laravel backend database anybody tell me how to do this one using laravel 8 thanks.
If your json object like this format -
{
owner:456123,
holders: [
{
equipments: 'test',
release_by: 'test'
},
{
equipments: 'test',
release_by: 'test'
}
]
}
This process you can follow.
Create a holder model. This holder model have relation with owner model.
After creating the relationship, you need to create a controller for consume the request. This controller must have a FormRequest for validating your inputs.
In this controller, you can insert your data using batch operation. How to bulk insert in laravel
I think it will help you for farther working process.
I suppose you have the table and have only columns named equipments and release_by other than id (which must be autogenerated) ,created_at and updated_at you can use
table::insert($array) if you have other columns and they are nullable or you have to insert some other values you can use
$newData = new Table;
foreach ($array as $data) {
$newData = new Table;
$newData->equipments => data['equipments'];
$newData->release_by => data['release_by'];
$newData->save();
}
I am using react-admin's useGet... query to gather data from my rails backend. The main problem here is the filter property (the last pair of curly braces in the useGetList operation). How can i filter the data by Dates (like get only transactions of the last month etc.)
This is the current (working) approach:
const { data, loading, error } = useGetList(
'transactions',
{ page: 1, perPage: 10000 },
{ field: 'id', order: 'ASC' },
{},
)
if (loading) return <p>Loading</p>
if (error) return <p>Error</p>
if (!data) return null
The entries in the database all have a createdAt and updatedAt property.
My approach would be to create a filter like this:
// constraints could be dates that I can easily set beforehand
{
{'createdAt', desiredLowerTimeConstraint, operation: '<='},
{'createdAt', desiredUpperTimeConstraint, operation: '<='}
}
The react-admin documentation is quite sparce with the filter property, I couldn't find good examples for how these objects are supposed to look like.
It all depends on how your API expects filters to look like.
For instance, in REST APIs served by JSONServer, a _lte suffix on a query string parameter name indicates that you want results "Less Than or Equal to" the value:
GET /transactions?createdAt_lte=2019-12-05
Provided you use the ra-data-simple-rest, you can craft this request by passing the parameter in the filter:
const { data, loading, error } = useGetList(
'transactions',
{ page: 1, perPage: 10000 },
{ field: 'id', order: 'ASC' },
{ createdAt_lte: '2019-12-05' },
)
If your API behaves differently, then you may use the same syntax for useGetList, and transform the parameter in your dataProvider before it's sent to the API.
I would like to group records and then filter them. When the code below is executed the records are grouped and during the loading of the grid, I can see the records being filtered by the condition, however, when the loading is finished, all (unfiltered) records are displayed.
Is there any way to make this work as expected?
Code:
afterrender: function(){
var myStore = this.myStore;
myStore. load({
callback: function(records, operation, success){
myStore.group('status', 'DESC');
if(recID != 0){
myStore.filterBy(function(record){
return record.get('count') > '10' ;
});
}
}
});
}
I am using backgrid.js with backbone.js. I'm trying to populate JSON (user list) in backgrid. Below is my JSON,
[{"name": "kumnar", "emailId":"kumar#xxx.com",
"locations":{"name":"ABC Inc.,", "province":"CA"}
}]
I can access name & emailId as below,
var User = Backbone.Model.extend({});
var User = Backbone.Collection.extend({
model: User,
url: 'https://localhost:8181/server/rest/user',
});
var users = new User();
var columns = [{
name: "loginId",
label: "Name",
cell: "string"
}, {
name: "emailId",
label: "E-mail Id",
cell: "string"
}
];
var grid = new Backgrid.Grid({
columns: columns,
collection: users
});
$("#grid-result").append(grid.render().$el);
userEntities.fetch();
My question is, how do I add a column for showing locations.name?
I have specified locations.name in the name property of columns but it doesn't work.
{
name: "locations.name",
label: "E-mail Id",
cell: "string"
}
Thanks
Both backbone and backgrid currently don't offer any support for nested model attributes, although there are a number of tickets underway. To properly display the locations info, you can either turn the locations object into a string on the server and use a string cell in backgrid, or you can attempt to supply your own cell implementation for the locations column.
Also, you may try out backbone-deep-model as it seems to support the path syntax you are looking for. I haven't tried it before, but if it works, you can just create 2 string columns called location.name and location.province respectively.
It's really easy to extend Cell (or any of the existing extensions like StringCell). Here's a start for you:
var DeepstringCell = Backgrid.DeepstringCell = StringCell.extend({
render: function () {
this.$el.empty();
var modelDepth = this.column.get("name").split(".");
var lastValue = this.model;
for (var i = 0;i<modelDepth.length;i++) {
lastValue = lastValue.get(modelDepth[i]);
}
this.$el.text(this.formatter.fromRaw(lastValue));
this.delegateEvents();
return this;
},
});
In this example you'd use "deepstring" instead of "string" for your "cell" attribute of your column. Extend it further to use a different formatter (like EmailFormatter) if you want to reuse the built-in formatters along with the deep model support. That's what I've done and it works great. Even better is to override the Cell definitions to look for a "." in the name value and treat it as a deep model.
Mind you, this only works because I use backbone-relational which returns Model instances from "get" calls.
I'm currently working with SQL Server and Sequelize on a project. I'm facing a problem when trying to insert rows in a table that has a trigger associated with it.
I was able to find this information about the problem https://github.com/sequelize/sequelize/issues/3284
I have downloaded the .js files and what it says is that it works with a temp table to overcome this issue:
SequelizeDatabaseError: The target table 'tableA' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.
This solution is not working for me, I have issues when trying to re use the object that were interseted and can't access to all of the variables.
Is there any other walk around for this issue?
This is my code:
return Person.create( { lastName: options.lastName,
name: options.name,
}, {transaction: t}).then(
function (person) {
return Student.create({ id :person.id,
numberRecord :'PF-'+person.id,
}, {transaction: t}).then(function (student) {
return Shoots.create({ id : person.id,
numberRecord :'PF-'+person.id
}, {transaction: t}).then(function (record) {
return StudentContract.create({
PersonID :person.id,
dateStart:'2016021 00:00:00.000',
endDate:'20161231 00:00:00.000',}
, {transaction: t}).then(function (contract) {
return student;
});
});
});
});
}).then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback is
console.log(result);
cb(null, result);
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback is
console.error(+err);
cb(err, "Transaction Failed");
});
When using the solution provided in the link above, it says that it can't find the 'id' property. 'id' is defined in Person and Student as the primary key.
Any ideas or official solution from sequelize to work this issue?
Thanks!
Set hasTrigger to true when defining the table, and Sequelize will take care of this for you. For example:
sequelize.define(TABLE_NAME,
{
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true
},
description: {
type: Sequelize.VARCHAR(8000)
}
},
{
// options
timestamps: false,
hasTrigger: true
});
I have read your question and I understood that you need to capture what you inserted. And yes it is possible to capture the inserted data with table variable.
Try below, hope it will help atleast to get some other idea.
DECLARE #IDENTITY_CAPTURE TABLE (INSERTED_IDENTITY BIGINT, INSERTED_BASE_NME VARCHAR(20))
INSERT INTO SALESORDER
OUTPUT INSERTED.SALESORDER_NBR, INSERTED.SALESORDER_NME INTO #IDENTITY_CAPTURE
SELECT 1, 'PICSART'
UNION ALL
SELECT 2, 'IMAX'
Then you can query on Table variable to get the data inserted.
Thank you