here is the data
req[
{
"_id": "123",
"_ar_id": "111",
"_val": "10",
"ar_date": "2023-02-02"
},
{
"_id": "1234",
"_ar_id": null,
"_val": "0",
"ar_date": "2023-02-02"
}
]
$validator = Validator::make($req->all(), [
'req.*._id' => 'required',
'req.*._val' => 'required',
'req.*.ar_date' => 'required',
]);
if ($validator->fails()) {
return response()->json(['stat' => 0,'msg' => $validator->errors(), 'data' => "Please input required details" ]);
}
since i validate the request, how can i use it in where to ?
here's the code
$record = ArRecordCount::where('ar_list_id', $list_id)->where('ar_date', $ar_date)->first();
and if has record then it will update, if no record then delete
Related
I have a such json:
{
"id":1,
"name":"some name",
"decorations":[
{
"id":1,
"options":[
{
"type":"color",
"label":"Color",
"items":[
{
"label":"Green shades",
"color_group":{
"shade":"green",
"name":"Green"
},
"colors":[
{
"rgb":"249,249,249",
"hex":"#FBFBFB"
},
{
"rgb":"249,249,250",
"hex":"#FBFBFF"
}
]
},
{
"label":"White shades",
"color_group":{
"shade":"white",
"name":"White"
},
"colors":[
{
"rgb":"255,255,255",
"hex":"#FFFFFF"
}
]
}
]
},
{
"type":"position",
"label":"Position",
"items":[
{
"label":"Front"
},
{
"label":"Back"
}
]
}
]
}
]
}
which I want to validate, rules are:
id, name, decorators required
each decorator should have id and options
each option should have type, label and items. each option has few types, all of them has same structure of each item except of a color type
so each item should have label, and in case of color option each item should have color_group, colors
- where color_group should have 'shade' and name
- each color in colors should have rgb and hex
I want to use symfony assertions to validate that json, and all is more or less straightforward except that rule where depends on a type I have different rules... I do not know how to do it right, I found that I can use expression or callback assertion but in both case it showing how to validate object but not a array.
Here is what I have:
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
'id' => [
new Assert\Required(),
new Assert\Type(['type' => 'integer']),
],
'decorations' => new Assert\All(
[
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
'id' => [
new Assert\Required(),
new Assert\Type(['type' => 'integer']),
],
'options' => new Assert\All(
[
'type' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
],
'label' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
],
'items' => new Assert\All(
[
new Assert\Collection(
[
'allowExtraFields' => true,
'fields' => [
// label is required for all types
'label' => [
new Assert\Required(),
new Assert\Type(['type' => 'string']),
]
// here I should add constraints for colors, if type equal to "color", but do not know how to make that if/else statement...
]
]
)
]
)
]
)
]
]
)
]
)
]
]
);
I am using Angular as front end and parameters that is send to post in laravel is array of objects. I want to insert the bulk of elements without using loop. Is is possible?? If yes how can I achieve that??
RoomAvailability Controller in Php
public function storeRoomAvailability()
{
$roomId = request();
$test = RoomAvailability::where('room_id', $roomId->room_id)->get();
if (is_null($test)) {
return response()->json([
'success' => false,
'message' => 'The room is already taken'
]);
} else {
$roomAvailability = RoomAvailability::create([$this->validateRequest()]);
return response()->json([
'success' => true,
'message' => 'Room Availability has been created successfully.',
'data' => $roomAvailability
]);
}
}
private function validateRequest()
{
return request()->validate([
'room_id' => 'required |unique:room_availabilities',
'reservation_id' => 'nullable',
'booking_id' => 'required',
'check_in_date' => 'required',
'check_out_date' => 'required'
]);
}
Parameters from Angular 8
[
{
"reservation_id": 25,
"room_id": 1,
"check_in_date": "2020-04-27 12:00:00",
"check_out_date": "2020-04-30 12:00:00",
"availability": false,
"status": "booked",
"booking_id": 26
},
{
"reservation_id": 26,
"room_id": 2,
"check_in_date": "2020-04-27 12:00:00",
"check_out_date": "2020-04-30 12:00:00",
"availability": false,
"status": "booked",
"booking_id": 26
}
]
first convert the parameters that comes from angular to associative arrays
$arrayValues=json_decode($Parameters_from_Angular,true);
then, insert the new values using bulk insert:
RoomAvailability::insert($arrayValues);
I have the following function which searches for results in elasticsearch.
I want to do the following request with PHP and Guzzle.
/**
* {#inheritdoc}
*/
public function sendSearchRequest($es_node, $request) {
try {
if (isset($es_node)) {
$ssl = $es_node->get('field_ess_verify_ssl')->value;
$ssl_val = $ssl ? 'https://' : 'http://';
$body = [
'json' => [
'query' => [
'bool' => [
'should' => [
[
'multi_match' => [
'query' => $request->get('search'),
'fields' => ['message', 'event.type'],
'operator' => 'AND',
],
],
[
'match' => [
'event.type' => $request->get('type'),
],
],
[
'match' => [
'event.labels' => $request->get('label'),
],
],
],
],
],
],
];
$response = $this->httpClient->request('POST', $ssl_val . $es_node->get('field_ess_host')->value . ':' . $es_node->get('field_ess_port')->value . '/***/***/_search?pretty', $body)
->getBody()
->getContents();
return json_decode($response, TRUE)['hits']['hits'] ?? '';
}
} catch (Exception $exception) {
\Drupal::logger(__METHOD__)->error('No ES node has been found.');
return FALSE;
}
}
But with this i get a parsing exception which means that the multi_match doesn't work this way. If i use a 'normal' match in that place it's working fine but then i am limited to 1 field.
The other match fields uses other form fields for input and that has always one value.
But with form field 'Description search' i want to look in multiple fields with the AND operator.
Anyone knows how to fix this?
Screenshot of the form:
Your query looks good to me and ran in my test. It should work as is.
But in the interest of future readers, this is how multi-match queries work with elastic search.
GET /_search
{
"query": {
"multi_match" : {
"query": "Will Smith",
"type": "best_fields",
"fields": [ "first_name", "last_name" ],
"operator": "and"
}
}
}
Note that this example from the elastic search documentation is identical in nature to your section of the code above:
...
'multi_match' => [
'query' => $request->get('search'),
'fields' => ['message', 'event.type'],
'operator' => 'AND',
],
...
This is how multi-match queries are formed, and you did it.
I am trying to integrate search by date range with PHP and elastic search
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'from' =>0,
'size' => 50,
'body' => [
'query' => [
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
]
]
];
$filter_date=array();
$filter_date['range']['datecreatedsort']['gte']='2015-11-27';
$filter_date['range']['datecreatedsort']['lte']='2017-11-27';
$params1['body']['query']['filtered']['filter']=$filter_date;
$params1['body']['sort']['datecreatedsort']['order'] = 'desc';
try {
$results = $client->search($params1);
//print_r($results);
}
catch (Exception $e) {
$last = $client->transport->getLastConnection()->getLastRequestInfo();
$last['results']['error'] = [];
print_r($last);
}
When I am running above query I am getting following error
[query_string] malformed query, expected [END_OBJECT] but found
[FIELD_NAME]","line":1,"col":78},"status":400}
datecreatedsort filed mapping is date type is date and value in
elastic search db is "datecreatedsort":"2016-05-30T09:39:40.000Z"
please help where is the issue in elastic query.
It's the native elasticsearch request.
In PHP you need to create array query with the same structure.
GET /joborders/_search?pretty=true
{
"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2017-11-22 13:49:00",
"lte": "2017-11-22 23:50:00"
}
}
}
]
}
}
PS You need to pass date ranges in the same format as datecreatedsort in db ("2016-05-30T09:39:40.000Z")
try this:
{
"query": {
"filtered": {
"query": {
"query_string": {
"default_field": "description",
"query": $wildCardString
}
},
"filter": {
"range": {
"datecreatedsort": {
"gte": '2015-11-27',
"lte": '2017-11-27'
}
}
}
}
}
}
your code for the query will look something like this:
$query=array(
'filtered'=>array(
'query' => array(
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
),
'filter'=>$filter_date
)
);
Hi Got the solution its working fine
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'body' => [
'query' => [
'bool' => [
'filter' => [
'range' => [ 'date_modified' => ['gt'=>$duration,'lt'=>$today,'boost'=> '2.0'] ]
],
'must' => [
'match' => [ 'description' => $wildCardString ]
]
]
]
]
];
Thanks guys for help.
My Request File rules are
public function rules()
{
return [
'mobile' => 'required',
'code' => 'required',
];
}
My input data can be a simple => for which the request validation is working fine.
{
"mobile":"81452569",
"code":"4858"
}
My input data can be a complex too => for which the request validation is not working fine.
[{
"mobile":"81452569",
"code":"4858"
},
{
"mobile":"81452570",
"code":"4858"
}]
How to validate for multiple rows with request.
I would sent it all in array to the request object like so:
"data" => [
[
"mobile" => "81452569",
"code" => "4858"
],
[
"mobile" =>"81452570",
"code" =>"4858"
]
];
Then in your validation rules do this:
public function rules()
{
return [
'data.*.mobile' => 'required',
'data.*.code' => 'required',
];
}