I want to know whether any value of the column fileType has the value of $fileType. I want to give an error msg if there is no atleast 1 such row. I'm getting a wrong but big output for dd($data); here. May i know how to accomplish this correctly
$data=Filesizes::where('FileType',$fileType);
dd($data);
die();
if($data){
Uploads::create($dataToInsert);
}
else{
return response()->json(['status'=>'Failed']);
}
You have use first or exists
$data=Filesizes::where('FileType',$fileType)->exists();
if($data){
Uploads::create($dataToInsert);
}
else{
return response()->json(['status'=>'Failed']);
}
You need to add ->get(), ->first() or ->exists() to actually retrieve your results, at the moment $data is a Query Builder object and not a collection of Filesizes.
I'd recommend Filesizes being singular too, i.e. Filesize, as that tends to be the convention for model names, same for Uploads.
$data = Filesizes::where('FileType', $fileType)->get();
if ($data->count() > 0) {
Uploads::create($dataToInsert);
} else {
return response()->json(['status'=>'Failed']);
}
Use first() of count() to check record exist or not:
$data=Filesizes::where('FileType',$fileType)->first();
if($data){
Uploads::create($dataToInsert);
}
else{
return response()->json(['status'=>'Failed']);
}
By count()
$data=Filesizes::where('FileType',$fileType)->count();
if($data > 0){
Uploads::create($dataToInsert);
}
else{
return response()->json(['status'=>'Failed']);
}
You ca use the result you already have, you dont need to do a second seperate query to get the maxFileSize
$maxFileSize = Filesizes::where('FileType',$fileType)->value('MaxFileSize');
if($maxFileSize !== null){
Uploads::create($dataToInsert);
} else {
return response()->json(['status'=>'Failed'], 422);
}
Related
I have an object of $person as below:
$person = Person::where('id', $id)->first();
According to which $person exists or not I load other data:
if($person) {
$person->family_members = FamilyMemberController::FamilyMemberOf($person->id);
} else {
$person->family_members = [];
}
In the view file, I check the $person->family_members if not empty and exists to add a generated value :
if(!empty(array_filter($person->family_members))) {
// my code
}
But it throws an error:
array_filter(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given
I need to check this $person->family_members to make sure whether it's an array or a collection is not empty.
Writing code for if array do something if collection do something is the wrong way of implementation.
You can do two things.
use both returns as collection()
or either use both returns as an array[]
If collection
else {
$person->family_members = collect();
}
If array
use ->toArray() at the end of Eloquent. Check this answer
As well, I think you are confused with array_filter(). Maybe you are searching for in_array() or contains()
Use count method
if(count($person->family_members)>0){
//your code
}
We don't know your code, but given the error, it's safe to assume your method returns a Collection. You can see available methods in the docs. However, if you need it as array, all you need to do is call ->toArray() on the result Collection. Then you can use array_filter.
What about just doing
if(!$person->family_members){
// your code here
}
or
if($person->family_members){
// your code here
} else {
// code of "if it's empty" goes here
}
You can use the count() function to return a count of an index. ex
if(count($person->family_members)){
//do your true algo
}
Why you are using the empty method ?! Try this:
$person->family_members = $person ? FamilyMemberController::FamilyMemberOf($person->id) : null;
if($person->family_members){ // your code }
Try simple ways in Programming ;)
I created this service method in Angular JS which checks if an array of potential statuses(pendingApplications) match any of an array of set statuses(applicableStatuses). For this to work it meetsStatusCondition should return true after the first match occurs. Only 1 of the numbers in pendingApplications array needs to match and I'd like to end the execution of this function. Currently it's looping through every item in pendingApplications array
`containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.forEach(function(status) {
if (applicableStatuses.includes(status)) {
return pendingApplications.meetsStatusCondition = true;
}
});
}`
This is a limitation with .forEach, you can't break out if it like you can with a for loop
Just a regular for loop will work
for (const status of applicableStatuses){
if (applicableStatuses.includes(status)) {
pendingApplications.meetsStatusCondition = true;
break //or return if you want to exit out of the enclosing function instead of just the loop
}
}
Often when you want to short-circuit a forEach like this, what you're really looking for is another method like find() or some().
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.meetsStatusCondition = pendingApplications.some(function(status) {
return applicableStatuses.includes(status)
});
}
There is no point in using forEach (which doesn't have a breaking option) if you could just use a regular for ... of loop instead:
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
for (const status of pendingApplications) {
if (applicableStatuses.includes(status)) {
pendingApplications.meetsStatusCondition = true;
break;
}
}
}
However, even this seems a bit too complicated, you could just set meetsStatusCondition to the result of some:
containsApplicableStatus: function(pendingApplications, applicableStatuses) {
pendingApplications.meetsStatusCondition =
pendingApplications.some(status => applicableStatues.includes(status));
}
I do wonder if it makes sense to set a non-index property on your array though, maybe rethink that. This works but it's usually not something you'd expect on an array, and it will be lost if you convert that array to JSON for instance.
In my code, I have to check an array contains a specific item or not.
Here is my code
If error.errors[0].peoples = .noteEntered {
Print("something")
}
To learn better, can I ask you to show me how to do it in the better way, I think it's not good.
Many thanks
You can try
if error.errors.contains(where:{$0.peoples == .noteEntered}) {
print("something")
}
If you need the item do
if let item = error.errors.first(where:{$0.peoples == .noteEntered}) {
print(item)
}
if error.errors[0].peoples.contains(.noteEntered) {
print("something")
}
how can i check if any javaScript object's property exists and if it exists then it has a valid value?
actually,i am a beginner and trying to solve this-
Check if the second argument is truthy on all objects of the first argument(which is an array of objects).i.e.
check if the second argument exists in all the objects in first argument(an array) as a property.
if it exists, it should not be-
invalid, as age can't be 0
null
undefined
empty string('')
NaN
till now i have this-
function truthCheck(collection, pre) {
for(var i=0;i<collection.length;i++){
if(!(pre in collection[i])||collection[i]
[pre]===undefined||isNaN(collection[i]
[pre])||collection[i][pre]===""||
collection[i][pre]===null||collection[i]
[pre]===0)
{
return false;
}
}
return true;
}
i know this is not the best wayto solve .Is there a better way to do this?i don't like that long if statement in my code.i have seen other SO links-link1,link2 but none of them seemed to solve my query. any kind of help is highly appreciated.
P.S. this code is not working for some true cases even.
o = new Object();
o.prop = 'exist';
if(o.hasOwnProperty('prop')){
if(o['prop']){
alert('good value')
}
}
https://stackoverflow.com/a/6003920/1074179
this is what i was looking for and absolutely logical-
for(var i in array){
if((prop in array[i])&& Boolean(array[i][prop]))
{
//do something
}
}
the Boolean() function is something which made my day. Learn more at this link.
Look at the below example.
let the json object be
var a = { obj1:"a",obj2:"b"}
to check if an object exists,you can use hasOwnProperty() method.
a.hasOwnProperty("obj2") //this will return true
a.hasOwnProperty("obj3") // this will return false
to check the value of an object
if(a["obj1"] && a["obj1"]!="" && a["obj"]!=0){
//place your logic here
}
I'm creating an Address Book program using array. I've done with the add and print data option. But now I'm stuck with the search/update option.
This is my code in searching for the element if it exist in my array or not.
public void update_data(){
String user_input_data;
int search_data = 0;
System.out.println("Enter the data that you want to search: ");
user_input_data = user_data.nextLine();
while(search_data<data_recorded){
if(user_input_data == AddressBook_Array_name[search_data])
{
System.out.println("Data found!");
}
else
{
System.out.println("Data not found!");
}
search_data++;
}
}
But when I run the program. It always return to false and print the else statement.
I don't know what's wrong with it. Anyway the data_recorded variable holds the number of data inputted by the user in the add option.
You need to use equals() instead of == in java for comparision.
if (user_input_data.equals(AddressBook_Array_name[search_data]))
Also, instead of the while you may want to use the foreach loop (removes the need for search_data variable).
for(String addressBookElem : AddressBook_Array_name) {
if (user_input_data.equals(addressBookElem)) {
System.out.println("Data found!");
return;
}
}
System.out.println("Data not found!"); // reaches this statement if data not present
i think you should use .equals function instead of ==.