Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

murilo's avatar
Level 10

Session is not working inside Rule in Laravel 6

I have in my Form Request , a Rule that Verify Captcha -

use App\Rules\VerifyCaptcha;

 public function rules()
    {

        return [
            'captcha'                   => ['required' , new VerifyCaptcha() ]
        ];
    }

verify VerifyCaptcha.php Rule File -

 public function passes($attribute, $value)
    {
       
        if($value == Session::get('captcha')){
            return true;
        }

        return false;
    }

Before , in Laravel 5.8 , this code used to works , but in Laravel 6 . SESSION IS NOT WORKING INSIDE THE RULE , I tried inside the form request as well . It does not work .

I tried ass well -

 public function passes($attribute, $value)
    {
       
        if($value == session()->get('captcha')){
            return true;
        }

        return false;
    }

If I print this in my controller , it works -

class SiteController extends Controller
{
    public function index()
    {
        dd(session()->get('captcha'));
    }
}

BUT INSIDE MY RULE IT IS NOT WORKING ANY MORE . IN LARAVEL 5.8 IT USED TO WORK .

0 likes
5 replies
Nakov's avatar

@murilo I just tested it and everything seems fine.

What do you have for a SESSION_DRIVER? I had a default one which is cookie in the new versions.

And how and where do you put the variable into the session?

Where do you have:

session()->put('captcha', 'some-value');

Make sure that the value you are putting is not null at the first place.

murilo's avatar
Level 10

hello @Nakov , I am adding like this

session()->put('captcha', 'my-captcha-name');

IT IS WORKING , BECOSE I CAN GET THIS SESSION FROM MY CONTROLLER LIKE THIS -


class SiteController extends Controller
{
     public function index()
    {
        dd(session()->get('captcha'));
      }
}

BUT IT IS NOT WORKING INSIDE THE RULE , IN LARAVEL 5.8 IT USED TO WORK , BUT NOW IT IS NOT WORKING ANY MORE .

Nakov's avatar

@murilo no need to yell my friend. I tried it on Laravel 6.9 ..

Route::get('/index', function () {
    session()->put('testing', 'test message');
    return 'test';
});

I first visit this enpoint in order to set the session message. Then created a Rule, and in the passes method:

dd(session('testing'));

I got the test message in the browser. So make sure that your browser is not clearing the session on each request, and that you use a proper Session driver, you can check that as I already said in your .env file.

murilo's avatar
Level 10

hello @Nakov , I didnt yell . I typed uppercase just to stay more beutfull to read the code . Thanks for the help . I cant Access the session inside the form Request -

use Session;
class SendMessageRequest extends FormRequest
{

     public function authorize()
    {

        return true;
    }

 public function rules()
    {
        dd(Session::get('captcha'))
        return [
          // just a example , the session captcha will be not acced from Form Request , is Allways Null 
        ];

    }
}

It will be not possible

Nakov's avatar

@murilo and if you put the full path for the Facade, still the same?

use Illuminate\Support\Facades\Session;

Please or to participate in this conversation.