Friday, November 27, 2015

Creating Page Templates in Wordpress

Sometimes we want a template that can be used globally by any page, or by multiple pages.

To create a global template, write an opening PHP comment at the top of the file that states the template’s name.
1
<?php /* Template Name: Example Template */ ?>
It’s a good idea to choose a name that describes what the template does as the name is visible to WordPress users when they are editing the page. For example, we could name your template Homepage, Blog, or Portfolio.
This example from the TwentyFourteen theme creates a page template called Full Width Page:
1
2
3
4
5
6
7
8
<?php
/**
 * Template Name: Full Width Page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
basics-page-templates-03Once we upload the file to our theme’s folder (e.g., page-templates), go to the Page > Edit screen in your admin dashboard.
On the right hand side under attributes we will see template.  This is where users are able to access our global page templates.

Friday, November 20, 2015

Laravel save method problem

Laravel save() method save data to database and return inserted data with inserted id . when we save another record after one using same object, it overwrite first record instead of creating new record .

Example

1. create AbcModel.php

public function Abc extend Model{

   public function createNew($data){
       $this->abc_id = $data->id;
        $this->name = $data->name;
     
   }
}

Note : Model Abc only save first record of $users array and in second times it update first record data with second record without creating second record in table.

Solution of above problem is create object of Abc Model inside createNew() method .

public function Abc extend Model{

   public function createNew($data){
    $abc = new Abc();
       $abc->abc_id = $data->id;
        $abc->name = $data->name;
     
   }
}


2. create AbcController.php
class NotificationController extends BaseController {

     public function createAbc (){
       
        $users = array(0 =>array('id' => '1','name'=>'Ravi')
                                  1 => array('id' => '2','name'=>'Ranjan'));

      $new = new Abc();
      foreach($users as $user){
           $new->createNew($user);
        }
     }

}

Friday, November 13, 2015

Debugging Queries in Laravel 5.1

Laravel provides a fantastic database query builder and ORM system named Eloquent. It makes running queries extremely simple and allows us to get records with minimal effort. However, there may be times when we  need to build an advanced query and would like to see the actual SQL being generated.

In this tutorial let’s go through some of the ways we can easily get access to this information. Starting with the simplest methods and moving into more powerful packages.

Before we jump in let’s build an Eloquent query for a fictitious bug report that has been assigned to us:

Issue #22321
Problem: Searching users is showing to many results
Steps to reproduce: I search for "John Doe " and 
I'm getting back hundreds of results. 

As we dig into the code, we find this query that seems to be where
the problem lies:

$results = User::where(function($q) use ($request) {
    $q->orWhere('email', 'like', '%john@example.org%');
    $q->orWhere('first_name', 'like', '%John%');
    $q->orWhere('last_name', 'like', '%Doe%');
})->get();

Can you already spot the problem? If not, don’t worry as we will
start debugging this and see what is actually going on.

Simple Query Debugging

The simplest method to see the query generated is by utilizing
a ->toSql() method. All that we need to do is replace the closing
->get() with ->toSql(). Then printing out the results with the dd(),
die and dump, helper.

Here is the updated query:

$results = User::where(function($q) use ($request) {
    $q->orWhere('email', 'like', '%john@example.org%');
    $q->orWhere('first_name', 'like', '%John%');
    $q->orWhere('last_name', 'like', '%Doe%');
})->toSql();
dd($results)

Running this in the browser will give us the following generated
SQL:

select * from `users` where (`email` like ? or `first_name` like ?
or `last_name` like ?)

This method is great for quickly seeing the SQL. However, 
it doesn’t include the query bindings, only a ? for where they are
to be inserted. Depending on the complexity of the bindings, this
may be enough information for youto debug it.

Listening For Query Events

The second option is to listen for query events on the DB object.
To set this up above the query add this little helper:

\DB::listen(function($sql) {
    var_dump($sql);
});

Now when you load the page in the browser you will get the same
 output as in the simple query debugging section:

select * from `users` where (`email` like ? or `first_name` like ?
or `last_name` like ?)

DB::listen is also more advanced, and it accepts two additional
parameters to give us access to the passed in bindings and the
time the query took to run:

\DB::listen(function($sql, $bindings, $time) {
    var_dump($sql);
    var_dump($bindings);
    var_dump($time);
});

Running this again will then display the following results:

string 'select * from `users` where (`email` like ? or `first_name`
 like ? or `last_name` like ?)' (length=89)
array (size=3)
  0 => string '%john@example.org%' (length=18)
  1 => string '%John%' (length=6)
  2 => string '%Doe%' (length=5)
float 35.63

As you can see, this gives us the query, the bindings, and the
 time the query took to run. With this, we can match up the 
bindings to the query.

Debugbar Package

Another viable option is to install the Laravel Debugbar package.
 Once installed you will get a heads up overview of your app that
 is displayed at the bottom of the browser. It’s a very 
full-featured package and will give you lots of insights into 
your application.

The part we want to focus is the query tab that outputs every
query ran to generate the page.

This gives us the full query with the bindings inserted into 
the correct place and a bonus of showing query hints to 
improve it.

Another advantage of having the query shown instantly like 
this is you can quickly spot areas that might not be 
eager loaded. This can lead to a significate database 
performance hit, and it’s easy to notice as you will 
see the total number of queries being high.

Conclusion

Have you spotted the error with the query now? Instead of using
orWhere we needed to be using where. Here is the final code 
that fixes the bug:

$results = User::where(function($q) use ($request) {
    $q->where('email', 'like', '%john@example.org%');
    $q->where('first_name', 'like', '%John%');
    $q->where('last_name', 'like', '%Doe%');
})->get();

Which gives us the corrected query:

select * from `users` where (`email` like '%john@example.org%' 
and `first_name` like '%John%' and `last_name` like '%Doe%')

Friday, November 6, 2015

Add extra field in migration after creating table in Laravel 5.1

Hello Everyone,

In this blog we will discuss about add extra field in migration after creating table.

First time we create a table's migration & execute migration command. After this , we realize that we have to add a new field in database.

For this, we can modify table by this easy way.

Lets suppose that we have to add email field in users table.
Firstly run this command for create a new migration file.

php artisan make:migration add_email_in_users_table


It will create a new file in migration file. Write in its up() function the following lines.

        Schema::create('user_details', function ($table) {
            $table->string('email');
          });

and in down() function write the following lines:

Schema::create('users', function ($table) {
            $table->drop_column('email');
          });

Make sure you put the same table name which you want to edit.

Now run migration command for this file.