VLS
VLS

Reputation: 445

Adding token to view make laravel

I have link like this with token:

<a href="{{ URL::to('/users/submit/' . $id->id) }}?_token={{ csrf_token() }}">Submit New</a>

which produce url:

http://example.com/users/submit/20?_token=fpf0LwHyf0JGBg0fnixjRFo1B5GgUM3RDp6dVgUU

Now in my controller I've added condition which check one column in database and based on this is returning different views.

public function wrongIdSubmit($Id) {

    $submits = self::$user->where('submit_id', $Id)->first();

    if (!$txid) {
        App::abort(404);
    }       

    if($submits->submit_id > 3) {
        return View::make('fail',[              
            'submits' => $submits               
        ]);         
    }

    else {
        return View::make('submit',[            
            'submits' => $submits           
        ]);
    }
}

My question is how to pass this token ?_token={{ csrf_token() }} to return View::make along with $submits variable? Because like is now I've got error

production.ERROR: Illuminate\Session\TokenMismatchException

Upvotes: 1

Views: 1937

Answers (2)

Gautam Patadiya
Gautam Patadiya

Reputation: 1412

Laravel provide function which returns you direct input hidden field with token.

csrf_field()

It will Generates an HTML hidden input field containing the value of the CSRF token.

so you can try like this:

return View::make('fail',[              
   'submits' => $submits,
   'token' => csrf_field()               
]); 

and in view just print:

{!! $token !!}

or direct also like:

{!! csrf_field() !!}

out put will be like:

<input type="hidden" name="_token" value="*****" />

Best of luck..

Upvotes: 2

Niraj Shah
Niraj Shah

Reputation: 15457

You must add the token to the form itself. You cannot pass it in the URL. Add the following to your form:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

Upvotes: 2

Related Questions