Laravel complains for missing non-nullable value, but it is there - json

I have the following code in Laravel 5.4.* to store an array of JSON objects.
Sample::where('contest_id', $request->get('contest_id'))
->where('type', '0')
->delete();
if ( $request->get('extra3') ) {
$samples = $request->get('samples');
//return $samples;
Sample::insert($samples);
}
I had an error for a missing value that is not nullable, so I started the debugging.
This is the return of the $samples, just before the encoding:
[
{
"id": 16,
"contest_id": "35",
"product_id": "2",
"supplier_id": "2",
"quantity": "5",
"type": "0",
"created_at": null,
"updated_at": null
},
{
"contest_id": 35,
"type": "0",
"product_id": "4",
"supplier_id": "3",
"quantity": 3
}
]
However, if I commented out the return and let the eloquent runs, the insert is messed up.
This is the full error
{"error":{"message":"SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: samples.supplier_id (SQL: insert into \"samples\" (\"contest_id\", \"created_at\", \"id\", \"product_id\", \"quantity\", \"supplier_id\", \"type\", \"updated_at\") select 1 as \"contest_id\", as \"created_at\", 5 as \"id\", 2 as \"product_id\", 5 as \"quantity\", 2 as \"supplier_id\", 0 as \"type\", as \"updated_at\" union all select 1 as \"contest_id\", 1 as \"created_at\", 1 as \"id\", 1 as \"product_id\", 0 as \"quantity\", ? as \"supplier_id\", ? as \"type\", ? as \"updated_at\")","code":"23000","status_code":500}}
Edit: A few more details how I reproduce the error. I don't have it on every insert request, but only after a certain process.
I have a dynamic form with Angular where user can create as many models of Sample as she wants. The first time the user send the request with the models, Laravel saves those without an error.
When I refresh the page and the form is created automatically with the data that I had in the DB, if I remove one of them and create a new one, but keep at least one of the previous data, then I have the error.
Just before the above code, I have a line that delete data from the same table, so everything that was there, isn't during the insert command.
HTML of the dynamic form:
<div class="row col-md-12" *ngFor="let input of initSamples; let i=index">
<div class="col-md-4">
<label *ngIf="i==0" for="product{{i}}" class="col-md-12">Product</label>
<div class="input-group">
<select name="product{{i}}" [(ngModel)]="input.product_id" id="product{{i}}" class="form-control">
<option *ngFor="let product of products" value="{{product.id}}">{{product.name}}</option>
</select>
</div>
</div>
<div class="col-md-4">
<label *ngIf="i==0" for="supplier{{i}}" class="col-md-12">Supplier</label>
<div class="input-group">
<select name="supplier{{i}}" [(ngModel)]="input.supplier_id" id="supplier{{i}}" class="form-control ">
<option *ngFor="let supplier of suppliers" value="{{supplier.id}}">{{supplier.name}}</option>
</select>
</div>
</div>
<div class="col-md-2">
<label *ngIf="i==0" for="quantity{{i}}" class="col-md-12">Quantity</label>
<input type="number" min=1 step=1 [(ngModel)]="input.quantity" id="quantity{{i}}" name="quantity{{i}}" class="form-control">
</div>
</div>

Related

How to display complex object attributes in form fields in angular

I have a JSON object with the structure below which I want to display in a form for edit when I clicked on edit button on my table row.
json
{
"id": 123,
"name": "Paul",
"cars": [
{
"type": "toyota",
"year": "2013"
},
{
"type": "audi",
"year": "2010"
}
]
}
I need help on how to display the type of each cars object in my table file separated by comma(,). I got the object id and name displayed but not attributes in the cars array. How do I complete my .ts code so that the type of each car could show up in my table.
.ts
showRowDetail( data: any ) {
this.formData.controls.id.setValue( data.id );
this.formData.controls.name.setValue( data.name );
//how do I update here to set the cars: types
}
.html
<div class="form-group">
<label>ID:</label> <input type="text" class="form-control"
formControlName="id">
</div>
<div class="form-group">
<label>Name:</label><input type="text" class="form-control"
formControlName="name">
</div>
<div class="form-group">
<label>Cars:</label> <input type="text" class="form-control"
formControlName="cars">
</div>
I omitted the *ngFor table code, the showRowDetail function is used on the edit button, which when clicked it opens a modal that present the form where the object data are shown. data.id and data.name fields are working, just the cars type I needed help with.
how to display the "type" of each "cars" object in my table file
seperated by comma(,)
Generate comma separated type values from carsarray using map() and join().
showRowDetail( data: any ) {
this.formData.controls.id.setValue( data.id );
this.formData.controls.name.setValue( data.name );
let types = data.cars.map(car => car.type).join(", ");
this.formData.controls.cars.setValue(types);
}

HTML - split the array values dynamically using comma seperated

Using ng-repeat am showing array of values which am getting it from backend. But i want to split the specific values using comma seperated and displya it dynamically.
Please find the HTML code below.
<li ng-repeat="history in claimGroupingHistories">
<div class="row">
<div class="col-lg-4 col-md col-sm-4 col-xs-4">
<label>{{'claimgroup.history.oldvalueselected'|translate}}:</label>
</div>
<div id="historyOldValueSelected_{{$index}}" class="col-lg-8 col-md-8 col-sm-8 col-xs-8">{{history.oldValueSelected}}</div>
</div>
</div>
</li>
Here, in the {{history.oldValueSelected}} values will be like ABC1234, XYZ2345, IJK098. Instead of showing the value in single line. I want to split the value using comma seperated and display the value.
For Example :
Now, it showing like,
Old Value Selected : ABC1234, XYZ2345, IJK098
Expecting output like,
Old Value Selected : ABC1234
XYZ2345
IJK098
Splitting the array values dynamically using comma seperated.
Testdata :
[
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":null,
"newValueSelected":"Group by minimum claims, Customer Invoice or Repair Date",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-22T18:42:59.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims, Customer Invoice or Repair Date",
"newValueSelected":"Group by minimum claims",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-23T06:13:07.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims",
"newValueSelected":"Group by minimum claims, Customer Invoice No",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-23T06:14:54.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":"Group by minimum claims, Customer Invoice No",
"newValueSelected":null,
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-24T06:23:33.000Z"
},
{
"id":null,
"dealerCode":"T030",
"oldValueSelected":null,
"newValueSelected":"Customer Invoice No, Group by minimum claims",
"createdBy":"System Admin",
"createdAt":"2019-01-23T06:13:00.000Z",
"lastModifiedAt":"2019-01-24T06:23:58.000Z"
}
]
I am assuming history.oldValueSelected is a string, because if oldValueSelected was an array you could use ng-repeat like this:
<div layout="column">
<span ng-repeat="oldValue in history.oldValueSelected" ng-bind="oldValue"></span>
</div>
Now to oldValueSelected being a string. I can think of two options: You can either
split the values in the controller after you got your data
split it in ng-repeat
If you choose the second option, you can do the following:
<div layout="column">
<span ng-repeat="oldValue in history.oldValueSelected.split(', ')" ng-bind="oldValue"></span>
</div>
EDIT: I didn't get the expected output when I used class="row"
<li ng-repeat="history in $ctrl.histories">
<!-- class="row" didn't deliver the expected output-->
<div layout="row">
<div class="col-lg-4 col-md col-sm-4 col-xs-4">
<!-- For testing purposes - replace with your code-->
<label>{{history.name}}:</label>
</div>
<div layout="column">
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8" ng-repeat="oldValue in history.oldValueSelected.split(', ')">{{oldValue}}</div>
</div>
</div>
</li>
In the controller for testing purposes:
this.histories = [
{name: "number 1", oldValueSelected:"ABC1234, XYZ2345, IJK098"},
{name: "number 2", oldValueSelected:"ABC1234, IJK098"},
{name: "number 3", oldValueSelected:"IJK098"}
];
app.component.html
<div *ngFor="let hero of heroes.split(', ')">
{{ hero }}
</div>
app.component.ts
private heroes: string = "123123, 123123, 345354, 5675757";
Output:
123123
123123
345354
5675757

Not able to fetch key and value from nested JSON data in IONIC

I face a problem now that I am not able to fetch key and value from nested JSON data.Please help me I am doing any wrong.
<ion-item class="item item-thumbnail-left item-text-wrap" type="item-text-wrap" nav-transition="android" ng-repeat="data in hrINPROCandidateList | filter:search">
<p class="small-text blue-text ">{{data.cfname}}</p>
<div class="row" ng-repeat="(key,value) in data.note_detail_fields track by $index">
<div class="col field">{{key}}</div>
<div class="col field-info">{{value}}</div>
</div>
</ion-item>
JSON Data
{
"responseToken": 1,
"list": [
{
"candidate_id": "4",
"note_detail_fields": "{\"link\":\"View Interview Details\",\"job_title\":\"Sopra Executive\",\"Candidate\":\"Mark Ashton\",\"Interview_Type\":\"First Interview\",\"\":\"\",\"Duration\":\"30 minutes\",\"Date\":\"Wednesday, 23<sup>rd<\\/sup> November 2016\",\"Time\":\"09:00 to 09:30\",\"Location\":\"london\"}",
"cfname": "Idris",
"clname": "Alba"
},
{
"candidate_id": "506",
"note_detail_fields": null,
"cfname": null,
"clname": null
},
{
"candidate_id": "32",
"note_detail_fields": "{\"link\":\"View Shared Job\",\"job_title\":\"Manager\",\"Location\":\"London, United Kingdom\",\"Package\":\"800 - 850 per day GBP\"}",
"cfname": "Sajal",
"clname": "Agarwal"
}
],
"totalCount": "4",
"success": 1,
"status": null
}
View data in app
You re trying to loop through a string instead of an array, create a filter that parse the json contained in data.note_detail_fields
<div class="row" ng-repeat="(key,value) in data.note_detail_fields | fromJson track by $index">
<div class="col field">{{key}}</div>
<div class="col field-info">{{value}}</div>
</div>
app.filter('fromJson',function(){
return function(input){ return angular.fromJson(input); }
})

Angular ng-show won't eval correctly

So if my JSON array object returns this:
"Tools": [
{
"name": "Submit a Claim",
"position": 1,
"isOn": true,
"alert": null
},
{
"name": "My Recurring Claims",
"position": 2,
"isOn": true,
"alert": null
},
{
"name": "Online Enrollment",
"position": 3,
"isOn": false,
"alert": "Online enrollment is available for the upcoming plan year. Click here to enroll!"
},
And my ng-show html has this:
<div class="toolTile col-md-3" ng-show="Tools.name = 'Online Enrollment' && Tools.isOn==true ">
<a href="#/claimEnter">
<img class="toolIcon" src="ppt/assets/toolIcons/oe.svg">
<p>Online Enrollment</p>
</a>
</div>
<div class="toolTile col-md-3" ng-show="Tools.name = 'Submit a Claim' && Tools.isOn==true ">
<a ng-click="transId()" ng-href="{{ ppt.Globals.hasDebitCard ? '#/claimEnter' : '#/claimSub' }}" >
<img src="ppt/assets/toolIcons/submitclaim.svg" >
<p>Submit a Claim</p>
</a>
</div>
Why does it keep evaluationing to false? I have tried quite a few variations and the "Online Enrollments" should hide while the "Submit a Claim" should show.
Any ideas of what I may be doing wrong here?
Thanks much.
Hi Man You could not Post Code with Out Head and Tail. There were Some
Syntax Errors Which caused the trouble .
You Need to Provide a Plunker with Buggy Code at least for some help :-) .
When you got an array in object you need to iterate over it.
I have provided you a running Plunker :-
<div ng-repeat="tool in data.Tools">
<div ng-show="tool.name == 'Online Enrollment' && tool.isOn==false ">
Online Enrollment
</div>
<div ng-show="tool.name == 'Submit a Claim' && tool.isOn == true ">
Submit a Claim
</div>
</div>
Plunker :-
http://plnkr.co/edit/B01pKWLsUtA2JlTAPOd5?p=preview
Good Luck !! Let me Know Your Queries if any !!

Quiz app - comparing values from inputs and JSON after button click

I wanted to make simple quiz app.
In my JSON file I have question with answers and points for an answer.
I want to list questions, gives text inputs for user to fulfill and a button to check answers. After clicking button I will show result and mark with green or red questions which were answered correct or incorrect.
I am able to list questions but I do not know how after clicking button I could go through all inputs and do all operations: (check them with corresponding answers from JSON file, add points, mark fields).
My JSON file is:
[
{
"id": 1,
"question": "How much is it: 2+2",
"answer": ["4", "four"],
"points": 2
},
{
"id": 2,
"question": "How much is it: 2*2",
"answer": ["4", "four"],
"points": 3
},
{
"id": 3,
"question": "How much is it: 2+2*2",
"answer": ["6", "six"],
"points": 4
}]
HTML:
<div class="col-sm-4">
<h3>What are Your answers? </h3>
<form>
<div class="form-group" ng-repeat="item in answers">
<label>Question (Id: {{item.id}}): {{item.question}}</label><br>
<input type="text" name="{{item.id}}" id="{{item.id}}">
</div>
<button type="submit" class="btn btn-default" ng-click="calculatePoints()">Check</button>
</form>
</div>
<div class="col-sm-4">
<h3>Number of points which You have:</h3>
<span>{{e}}</span>
</div>
Controller:
$scope.calculatePoints = function() {
};
first you need to give ng-model to input
secondly you can pass answer element and answer to the calculatepionts function
<input type="text" ng-model="item.currentAnswer"
name="{{item.id}}" id="{{item.id}}">
sending the question object and answer to the calculate function
<button type="submit" class="btn btn-default"
ng-click="calculatePoints(item,item.currentAnswer)">Check</button>
Now you can compare the answer with the orignal one in calculatePoints()