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
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.  🙂

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 define Global Variable or Constant in Laravel

We often need to define global variables/constants for our application, like Status, Conditions, Countries (we can put all countries in table as it will be along array/object), also weekdays, weekends and e.t.c. vary on projects.

Status (Active, Inactive) and Conditions (Ok, Good, Better, Best) or Grade (F, C, D, A, A-, A+), so like these type of variables/constants,  we write same code multiple times. But if we set all of these as Global Variables then we can use these anytime, anywhere in application.



And here I am presenting how to define GLOBAL Variable/Constant in Laravel 5.2. (This is not the only way, but I like it)

  1. First create a file named constants.php on config directory
  2. And code some variables like below

return [

‘STATUS’ => [

1 => ‘Active’,

0 => ‘Inactive’


‘Conditions’ => [

1 => ‘Ok’,

2 => ‘Good,

3 => ‘Better,

4 => ‘Best’



‘GRADE’ => [

‘A+’ => ‘A+’,

‘A’ => ‘A’,

‘A-’ => ‘A-’,

‘B’ => ‘B’,

‘C’ => ‘C’,

‘D’ => ‘D’,

‘F’ => ‘F’



  1. Now, how can we use/access this Global Variables? Very easy, and we can also access these variables on view too.

Config::get(‘constants. STATUS’); // it will output status array.

Or, if we want a specific one element then

Config::get(‘constants. GRADE.F’); // output: F

  1. And lastly we can also set Global variable only for the current request. This will not persist.

Config::set(‘constants. Conditions.5’, ‘Not Stable’)


And finally – if you like this article please inspire me by posting a comment (at below) for the next article 😉 . And I don’t mind if you want to share among with your friends, bocz. Sharing’s Caring.


Credit: google, stackoverflow,, Laravel docs and etc.

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’)



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()


And dump it


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) {




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






Laravel 5.1: Unsupported operand types (FatalErrorException in …)

Ok. When I am upgrading my application from Laravel 5.0 to 5.1 I get an falat error that this showing “Unsupported operand types”


What did I do on Laravel 5.0:

I add two array with in + operator, and that is the problem. So, I looking for solution and get this on Laracast

$item_list = array(”=>’–Select–‘) + Model::lists(‘name’, ‘id’);

what do I do on Laravel 5.1

this is a quick fix: just add another function at the end of the line ( and just before of the ; )  ->toArray()

so, here is the updated code

$item_list = array(”=>’–Select–‘) + Model::lists(‘name’, ‘id’)->toArray();


Why this: the reason for this update.

In 5.1, in Colleciton.php, it uses this:

return new static(Arr::pluck($this->items, $value, $key));

instead of this:

return array_pluck($this->items, $value, $key);

which, in turn does this:

return (Arr::pluck($this->items, $value, $key));

So it looks like the new behavior is to return a collection vs an array, thus the need to specify the toArray()


credit: @scottlaurent