Home > Blockchain >  why $request->has(' ') not working in laravel?
why $request->has(' ') not working in laravel?

Time:06-15

i am using laravel framework ,for testing API's i am using postman.i have one registration api which receives an input and storing the data based on some checks ,it's not passing the condition of $request->has('phone') can you please help me how to resolve this issue..?

UserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
 
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'phone' =>'required|integer'
        ];
    }
}

controller.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserRequest;
use App\Models\User;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use JWTAuth;

class UserController extends Controller
{

    public function register(UserRequest $request){
        if($request->has('phone')){
         // some logic        
        }
        if($request->has('user_name'){
         //some logic
        }
}

CURL Request

curl --location --request POST 'http://127.0.0.1:8000/api/register' \
--header 'Cookie: XSRF-TOKEN=eyJpdiI6InlXRkRCSVJyQ2xpWDA3WnZmY3BnMXc9PSIsInZhbHVlIjoiOGM5TnJkUTNHRjBtZk81ZjE2dEo4L29YZ3lkbnVWTWdidmlQb0N0Z29tSERHU1EvaXBXbWlLa3RsdTljVzlrUVNjZU9xSDJPRzJQUXJjSDE5UThCYmtVMDlGV1hONVg2N0s2bWY4bGY2NEhidVczYXlPZVNRSS9zNjlCNWJ0WGIiLCJtYWMiOiI4MjczYzE1ZTAxNDJmYmNiYTI4OTIzZjlhYWIxYzJkMjZmNzk1NDFiOGZlODBiZjFmYmY2NzdjNjNiMTZmY2QyIiwidGFnIjoiIn0=; laravel_session=eyJpdiI6IkRyUTRVN0RSUStBK2pmakRnUDFmbmc9PSIsInZhbHVlIjoiWlNZWXVhVWlOSXhXOEhoNjNUQktyb1BEaEQvekhaWFphMVk5NVZKaFdzZ2VtL0FqVUlUMXJ4THJLSjdkZHhmYTA1eUUyeVNhdUp6UncwczN2OGZDcTVrOS9WVzJQRUxHbm40cnE3ZjJkd2ZZY0J1UlgvQWdyWTdJMVVnejY0cDgiLCJtYWMiOiJmYzdhZGMzZmY3MWRiYTc1YjZjZThjZWFiMzk0ZTBhMjM3ZGU3OGFjYTJmN2Q3MmU3ZDVlMjYwZTUyM2ZjYjVkIiwidGFnIjoiIn0=' \
--form 'phone="56788988"'

curl request headers

form-data

dd($request)

Illuminate\Http\Request {#43
  #json: null
  #convertedFiles: null
  #userResolver: Closure($guard = null) {#262
    class: "Illuminate\Auth\AuthServiceProvider"
    this: Illuminate\Auth\AuthServiceProvider {#150 …}
    use: {
      $app: Illuminate\Foundation\Application {#2 …}
    }
    file: "/home/payarc/SHARE-COST/lemonpeak_sharecost/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php"
    line: "96 to 98"
  }
  #routeResolver: Closure() {#270
    class: "Illuminate\Routing\Router"
    this: Illuminate\Routing\Router {#26 …}
    use: {
      $route: Illuminate\Routing\Route {#250 …}
    }
    file: "/home/payarc/SHARE-COST/lemonpeak_sharecost/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    line: "691 to 693"
  }
   attributes: Symfony\Component\HttpFoundation\ParameterBag {#45
    #parameters: []
  }
   request: Symfony\Component\HttpFoundation\InputBag {#44
    #parameters: array:1 [
      "phone" => "7901002572"
    ]
  }
   query: Symfony\Component\HttpFoundation\InputBag {#51
    #parameters: []
  }
   server: Symfony\Component\HttpFoundation\ServerBag {#47
    #parameters: array:27 [
      "DOCUMENT_ROOT" => "/home/payarc/SHARE-COST/lemonpeak_sharecost/public"
      "REMOTE_ADDR" => "127.0.0.1"
      "REMOTE_PORT" => "37410"
      "SERVER_SOFTWARE" => "PHP 7.4.16 Development Server"
      "SERVER_PROTOCOL" => "HTTP/1.1"
      "SERVER_NAME" => "127.0.0.1"
      "SERVER_PORT" => "8000"
      "REQUEST_URI" => "/api/register"
      "REQUEST_METHOD" => "POST"
      "SCRIPT_NAME" => "/index.php"
      "SCRIPT_FILENAME" => "/home/payarc/SHARE-COST/lemonpeak_sharecost/public/index.php"
      "PATH_INFO" => "/api/register"
      "PHP_SELF" => "/index.php/api/register"
      "HTTP_USER_AGENT" => "PostmanRuntime/7.28.4"
      "HTTP_ACCEPT" => "*/*"
      "HTTP_CACHE_CONTROL" => "no-cache"
      "HTTP_POSTMAN_TOKEN" => "a67d63c4-d560-4867-a066-5d9d81c38e99"
      "HTTP_HOST" => "127.0.0.1:8000"
      "HTTP_ACCEPT_ENCODING" => "gzip, deflate, br"
      "HTTP_CONNECTION" => "keep-alive"
      "CONTENT_TYPE" => "multipart/form-data; boundary=--------------------------727143787238577144588277"
      "HTTP_CONTENT_TYPE" => "multipart/form-data; boundary=--------------------------727143787238577144588277"
      "HTTP_COOKIE" => "XSRF-TOKEN=eyJpdiI6ImtJMkhNOXBvbjZYMzNFejRNbUpsemc9PSIsInZhbHVlIjoiT21ScElGOUprdnVVY2MxMFpreEVCVTlzS2RGM09Ed1ZQWk1IYnA3L1ZjK2ZoOXA0eUxwVSthRG53cWRydmVNZEJGNlVDUmM0V1NZTU1VOWNraE9yRWE3dGhLQXFubVZoczZ5TFgzSTlXK2UvUlRsTFpxNzgxeHE1d2loRXdWTnYiLCJtYWMiOiIzNGYyYTZjMWE2NDIxNjYxYTI1Nzc0YTI1OGIxMGRlNDhlOWEyZGVkZmI2ODNiM2M2ZmI4MTM2MjAxZjM0YTg0IiwidGFnIjoiIn0=; laravel_session=eyJpdiI6IkRqWXVYakNaRXVZdCtpUUlmMExZd1E9PSIsInZhbHVlIjoiZmJsQzVRWEhVM0IwYWVVeTZEMnRnQ3Qva1B6NWZLMkJYZWgvcHZONVYwd3VuZnN6cGYzZVJRMzdsUVZvSlJDR01jd1RxZHRNMCtRY3doZDRFZzlmc1MycTlvbDNKTUcyaE9wb2lZZDJqMTJKNmxZVjRGNVM1YzE5cHhjRklvU3MiLCJtYWMiOiI3MmU1OWJhOGEwNTFmYmE2OWQ4OWI1OGMzYTFiMDAyN2NlNWViY2MyMzBmNmIxODAyZThlODA3Y2NjZTYyYjRlIiwidGFnIjoiIn0="
      "CONTENT_LENGTH" => "170"
      "HTTP_CONTENT_LENGTH" => "170"
      "REQUEST_TIME_FLOAT" => 1655210722.3332
      "REQUEST_TIME" => 1655210722
    ]
  }
   files: Symfony\Component\HttpFoundation\FileBag {#48
    #parameters: []
  }
   cookies: Symfony\Component\HttpFoundation\InputBag {#46
    #parameters: array:2 [
      "XSRF-TOKEN" => "eyJpdiI6ImtJMkhNOXBvbjZYMzNFejRNbUpsemc9PSIsInZhbHVlIjoiT21ScElGOUprdnVVY2MxMFpreEVCVTlzS2RGM09Ed1ZQWk1IYnA3L1ZjK2ZoOXA0eUxwVSthRG53cWRydmVNZEJGNlVDUmM0V1NZTU1VOWNraE9yRWE3dGhLQXFubVZoczZ5TFgzSTlXK2UvUlRsTFpxNzgxeHE1d2loRXdWTnYiLCJtYWMiOiIzNGYyYTZjMWE2NDIxNjYxYTI1Nzc0YTI1OGIxMGRlNDhlOWEyZGVkZmI2ODNiM2M2ZmI4MTM2MjAxZjM0YTg0IiwidGFnIjoiIn0="
      "laravel_session" => "eyJpdiI6IkRqWXVYakNaRXVZdCtpUUlmMExZd1E9PSIsInZhbHVlIjoiZmJsQzVRWEhVM0IwYWVVeTZEMnRnQ3Qva1B6NWZLMkJYZWgvcHZONVYwd3VuZnN6cGYzZVJRMzdsUVZvSlJDR01jd1RxZHRNMCtRY3doZDRFZzlmc1MycTlvbDNKTUcyaE9wb2lZZDJqMTJKNmxZVjRGNVM1YzE5cHhjRklvU3MiLCJtYWMiOiI3MmU1OWJhOGEwNTFmYmE2OWQ4OWI1OGMzYTFiMDAyN2NlNWViY2MyMzBmNmIxODAyZThlODA3Y2NjZTYyYjRlIiwidGFnIjoiIn0="
    ]
  }
   headers: Symfony\Component\HttpFoundation\HeaderBag {#49
    #headers: array:10 [
      "user-agent" => array:1 [
        0 => "PostmanRuntime/7.28.4"
      ]
      "accept" => array:1 [
        0 => "*/*"
      ]
      "cache-control" => array:1 [
        0 => "no-cache"
      ]
      "postman-token" => array:1 [
        0 => "a67d63c4-d560-4867-a066-5d9d81c38e99"
      ]
      "host" => array:1 [
        0 => "127.0.0.1:8000"
      ]
      "accept-encoding" => array:1 [
        0 => "gzip, deflate, br"
      ]
      "connection" => array:1 [
        0 => "keep-alive"
      ]
      "content-type" => array:1 [
        0 => "multipart/form-data; boundary=--------------------------727143787238577144588277"
      ]
      "cookie" => array:1 [
        0 => "XSRF-TOKEN=eyJpdiI6ImtJMkhNOXBvbjZYMzNFejRNbUpsemc9PSIsInZhbHVlIjoiT21ScElGOUprdnVVY2MxMFpreEVCVTlzS2RGM09Ed1ZQWk1IYnA3L1ZjK2ZoOXA0eUxwVSthRG53cWRydmVNZEJGNlVDUmM0V1NZTU1VOWNraE9yRWE3dGhLQXFubVZoczZ5TFgzSTlXK2UvUlRsTFpxNzgxeHE1d2loRXdWTnYiLCJtYWMiOiIzNGYyYTZjMWE2NDIxNjYxYTI1Nzc0YTI1OGIxMGRlNDhlOWEyZGVkZmI2ODNiM2M2ZmI4MTM2MjAxZjM0YTg0IiwidGFnIjoiIn0=; laravel_session=eyJpdiI6IkRqWXVYakNaRXVZdCtpUUlmMExZd1E9PSIsInZhbHVlIjoiZmJsQzVRWEhVM0IwYWVVeTZEMnRnQ3Qva1B6NWZLMkJYZWgvcHZONVYwd3VuZnN6cGYzZVJRMzdsUVZvSlJDR01jd1RxZHRNMCtRY3doZDRFZzlmc1MycTlvbDNKTUcyaE9wb2lZZDJqMTJKNmxZVjRGNVM1YzE5cHhjRklvU3MiLCJtYWMiOiI3MmU1OWJhOGEwNTFmYmE2OWQ4OWI1OGMzYTFiMDAyN2NlNWViY2MyMzBmNmIxODAyZThlODA3Y2NjZTYyYjRlIiwidGFnIjoiIn0="
      ]
      "content-length" => array:1 [
        0 => "170"
      ]
    ]
    #cacheControl: array:1 [
      "no-cache" => true
    ]
  }
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/api/register"
  #requestUri: "/api/register"
  #baseUrl: ""
  #basePath: null
  #method: "POST"
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  -isSafeContentPreferred: null
  basePath: ""
  format: "html"
}

CodePudding user response:

Under Rules of your userRequest.php, u set integer validation but not defined the integer length. for example, if you processing the integer value between 1 to 10, do this

 public function rules()
{
    return [
        'phone' =>'required|integer|between:1,10'
    ];
}

CodePudding user response:

In postman headers you add content-type as a application\json and select as form-data in body section

After that please verify your passing the has check request key has matched to the form-data one.let me know if this will work

  • Related