lundi 11 juin 2018

How to validate json body in laravel

my sample json data is here

{
    "allRecords": [
        {
            "name": "Jhon",
            "email": "jhon@gmail.com",
            "age": "30",
            "address": "NA",
            "uniqueId": "81467yht"
        },
        {
            "name": "Doe",
            "email": "doe@gmail.com",
            "age": "60",
            "address": "NA",
            "uniqueId": "88797kkk"
        },
        {
            "name": "Falcon",
            "email": "falcon@gmail.com",
            "age": "40",
            "address": "NA",
            "uniqueId": "7876ooot"
        }
    ]
}

here allRecords will have more than one object. so i need to validate all allRecords body fields in laravel.

validation 1 : validate specific params is required like (name,email,age, uniqueId). if one params is missing from whole json object then it should be through a error message.

validation 2 : need to check unique value "uniqueId" with my existing database.

i have a function named "recordsUniqueIdCheck()" that can check uniqueId is unique or not from database.

function recordsUniqueIdCheck($uniqueId){
   if(exist id){
    return true;
   }else{
    return false
   }
}

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire