Set XSRF(/CSRF) Token Globally or TokenMismatchException in Laravel 5.* (for AJAX)

Very easy (পানির লাহান) 🙂

STEP – 1:

First we have to ad a meta tag (name=”_token”) in header

<meta name=”_token” content=”{{ csrf_token() }}” />

STEP – 2:

Then add below jQuery snippet before the </body> tag. Basically here we setting up the csrf token globally for ajax request. And after this we don’t need to send the csrf token on every ajax call.

$(function () {
// SET UP CSRF TOKEN Globally for Ajax Request
$.ajaxSetup({
headers: {
‘X-XSRF-TOKEN’: $(‘meta[name=”_token”]’).attr(‘content’)
}
});
});

STEP – 3:

And finally we have to add a method on “appHttpMiddlewareVerifyCsrfToken.php” file. So, add the below method on VerifyCsrfToken.php

/**
* Determine if the session and input CSRF tokens match.
*
* @param IlluminateHttpRequest $request
* @return bool
*/
protected function tokensMatch($request)
{
$token = $request->session()->token();

$_token = $request->input(‘_token’);

$header = $request->header(‘X-XSRF-TOKEN’);

return StringUtils::equals($token, $_token) ||
($header && StringUtils::equals($token, $header));
}

Now check and let me know your comments. And I don’t mind, if you want to share this article with your friends/colleagues. Bcoz. Sharing’s Caring.  🙂

Advertisements

Laravel: php artisan commands

Here I am trying to list all Laravel PHP Artisan commands one by one for my reference and your too.

 

– To create and update table by migration

php artisan make:migration Create_Table_Name  -> create a new table

php artisan make:migration Update_Table_Name_Column_Name -> update table

php artisan make:migration Add_Column_Name_Table_Name  -> add a new column to a table

 

– To create a Controller

php artisan make:controller pathControllerName –resource

path = directory/place of the controller, i usually create controller directory wise . i.e.: site/SiteController – all site related controller will store here and admin/AdminController – all admin related controller will store here or test/TestController – all test related controller will store here.

ControllerName = Name of the controller

–resource = a parameter to create controller with default CRUD functions (index, create, store, show, edit, update, destroy)

 

– To create a Model

php artisan make:model pathModelName

path = directory/place of the model (optional). Same as controller

ControllerName = Name of the model

 

How to print query or debug queries in Laravel

We often need to debug large queries in Laravel. And laravel have built-in functions almost for everything. Here is a simple query in laravel for example.

$data = User::where(‘status’, 1)

->select(‘name’, ‘username’, ‘email’, ‘password’)

->get();

 

but we want to see the sql query. So we have to use toSql() in the place of get() and to print the query we use dd() – die and dump function of laravel, so here is out update query

$data = User::where(‘status’, 1)

->select(‘name’, ‘username’, ‘email’, ‘password’)

// ->get()

->toSql();

And dump it

dd($data);

and the output is

“SELECT ‘name’, ‘username’, ‘email’, ‘password’ FROM user WEHRE status = 1”

 

n.b.: it also can be done by DB listen

DB::listen(function($sql) {

var_dump($sql);

});

 

Here is another option with Laravel DebugBar. It’s a full featured package and gives lots on insight of the application.

 

 

——————————————————————

Reff: https://scotch.io/tutorials/debugging-queries-in-laravel