Laravel find if number is between two different fields in a MySQL Database

Laravel find the number is between two different fields. Many times we find the record between two dates ‘from’ to ‘to’ date right. But how to find a number of between two fields. So it’s too easy in Laravel. So let’s see how to write `DB RAW` query in Laravel for find number between two fields.

Just used below simple query.

<?php
$itemExits = Availability::whereDate('status','=','1')
    	->whereRaw(\DB::raw(' 25 BETWEEN `from_no` AND `to_no` '));
$result = $itemExits->get();

As per above query, we get to record like greater than ‘from_no’ and lower than ‘to_no’ records from the database.

we can write two separate conditions for the getting same records like below.

<?php
$itemExits = Availability::whereDate('status','=','1')
    	->where('from_no','>=',25)
        ->where('to_no','<=',25);
$result = $itemExits->get();

We get the same result as above query both query return the same result.

If you face any issue after using the above query please write the comment below.

Enable Shortcodes in WordPress widget

WordPress by default does not allow to add shortcodes in the widget. So let’s see how to how to enable shortcode in WordPress widget. it’s too easy to allow shortcode in a widget. just follow below steps.

open functions.php  From your active theme. and add blow code for allow shortcode.

add_filter(‘widget_text’,’do_shortcode’);

Now you can a shortcode on widgets.

If you face any issue after adding this code write the comment.

How to properly use jQuery noConflict mode in WordPress

How to properly use Jquery noConflict mode because many times we face jQuery Conflict error. so let’s see how to write noConflict jQuery on WordPress.

So when you write jQuery on WordPress write a syntax like below.

(function($){

  $(document).ready(function(){
      // write code here
  });

  // or also you can write jquery code like this

  jQuery(document).ready(function(){
      // write code here
  });

})(jQuery);

Then write your jQuery code without Conflict you can write with ‘$’ or ‘jQuery’. So create a jQuery without conflict.

If you face any problem after using this code write the comment.

Create Category tree with PHP, WordPress

How to create a category tree with PHP from Array. Many times we required creating a parent and child category tree. So let’s make it easy. just used below function for creating category tree.

<?php
function buildTree($items) {
    $childs = array();
    foreach($items as &$item) $childs[$item['parent_id']][] = &$item;
    unset($item);
    foreach($items as &$item) if (isset($childs[$item['id']]))
            $item['childs'] = $childs[$item['id']];
    return $childs[0];
}

Passed your category array to `buildTree` function like below.

Assume the category array look like below

$items = array(
    array(‘id’ => 55, ‘name’ => ‘Child Category’, ‘parent_id’ => 2),
    array(‘id’ => 54, ‘name’ => ‘Child Category’, ‘parent_id’ => 2),
    array(‘id’ => 2, ‘name’ =>parent Category’, ‘parent_id’ => 0),
    array(‘id’ => 42, ‘name’ => ‘Child Category’, ‘parent_id’ => 1),
    array(‘id’ => 43, ‘name’ => ‘Child Category’, ‘parent_id’ => 1),
    array(‘id’ => 1, ‘name’ =>parent Category’, ‘parent_id’ => 0),
);

Now I passed this array to `buildTree` function for creating the category tree.

$output = buildTree($items);
print_r($output);

The output is below

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => parent Category
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 55
                            [name] => Child Category
                            [parent_id] => 2
                        )

                    [1] => Array
                        (
                            [id] => 54
                            [name] => Child Category
                            [parent_id] => 2
                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [name] => parent Category
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 42
                            [name] => Child Category
                            [parent_id] => 1
                        )

                    [1] => Array
                        (
                            [id] => 43
                            [name] => Child Category
                            [parent_id] => 1
                        )

                )

        )

)

The output gets something like this just used a single function for creating category tree.

If you face any issue after using this code please write a comment.

Laravel unique validation with record active condition

Laravel provides a good validation functionality we can easily set validation on Laravel. Laravel provides a unique validation for validating the unique value. many times we need to create a unique validation on many fields like SKU, slug etc…

So let’s see how to create a unique validation on Laravel.

public $rules = array(
    ‘name’ => ‘required|unique:product,slug|max:255,
);

I set unique validation on slug filed of product module. Now Laravel not allow to duplication value for a slug. So now assume I add a status field for products. When a status is 1 means the product is active and when status = 0 means the product is deactivated or deleted. Now I create a product with ‘simple-product’ slug after creating the product I delete that product now I never create a product with ‘simple-product’ slug every time I get a ‘Duplicate Record Found.’ validation so let’s create unique validation rules with status is the active condition.

public $rules = array(
    ‘name’ => ‘required|unique:product,slug,0,status’,
);

Now the unique validation fired when the record found with the active status = 1. the query fire like below.

select * from product where slug = ‘simple-product’ and status <>0;

Now unique validation search with active records only.

Now let’s see how to prevent unique validation on Edit method.change your validation rules on edit method like below.

$validate->rules[‘slug’]=”required|unique:product,slug,.$id.,id,status,1;

$id passed the edited product id.

Now the unique validation query fire like below.

select * from product where slug = ‘simple-product’ and id <> $id and status =1;

Now the unique validation checks the unique condition with the active product and not with the current product. so it’s too easy to create unique validation on Laravel.

If you face any issue after using this code please write a comment.

How to create a custom theme and use it in android application

Android – How to create the custom Android theme. let’s see how to create the custom material design theme as you want for your enterprise application. you can be used you custom color as you want on your application so let’s see how to customize the application background and font color as you want.

So let’s starts first of open your colors configuration file from “res/values/colors.xml” file and change or add the color as you want. My color configuration file as below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorText">#FFFFFF</color>
</resources>

Then after creating the new theme configuration file at “res/values/themes.xml” for creating new theme configuration like below.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="Theme.AppCompat.Light">

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:windowBackground">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/colorText</item>

    </style>

</resources>

The above is my theme configuration I set the “colorPrimary”, “colorPrimaryDark”, “android:windowBackground”, “android:textColorPrimary” for customize the App screen as i want.

The third step applies theme on your activity so let’s see first open “AndroidManifest.xml” file and change your custom configuration file like below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thecodingstuff.background">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:theme="@style/MyTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

As per above file, I add my custom theme on main activity like ‘ android:theme=”@style/MyTheme” ‘ You can add your custom theme on your activity for changing the app theme.

So you can customize the App background and font color as you want using the themes configuration.

If you face any issue after using this code please write the comment below.

Android – How to change the window background color

Android – How to change the activity window background color using “android:background”. there are two ways change the window background color. The first way you can change the window background color using your own theme. Follow this tutorial to create your own theme.  The second way you can change the window background color from your activity like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#4A148C"
    tools:context="com.thecodingstuff.first.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="The Coding Stuff"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

I just add “android:background” color on RelativeLayout you can add your custom background color as you want.

If you face any issue after using above code please write the comment below.

How to run Angular function on document ready?

How to run AngularJS function once the document is ready. Till now we used most famous JAVASCRIPT framework name is “jQuery”. The jQuery is open source framework. We can write all the jQuery functions on “document ready” so once all document is loaded then after running the jQuery code.

You all know how we can write the document ready on jQuery like below

(function($){

    $(document).ready(function(){
        // Write the jQuery function here
        alert("Document Ready");
    })

})(jQuery);

Above you can see how we write the document ready method on jQuery. We can write same document ready on AngularJs. So the function is run after all the js file is loaded. let’s see how we can write AngularJs function on document ready.

angular.module('MyApp', [])
    .controller('MyCtrl', [function() {
        angular.element(document).ready(function () {
            // write javascript/jQuery code here
            document.getElementById('message').innerHTML = 'Document Ready';
        });
    }]);

We see how we can write the javascript/jquery code on document ready using AngularJs APP. it’s too easy to write the document ready on AngularJs.

If you face any issue after using this code please write comment below.

 

How to clear git cache

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. How and when you need to clear Git cache. When you want to add any file/folder to git ignore at that time you need to clear git cache. When you think your git is messed up and acting funky you need to clear git cache. Just follow below command for the clear cache.

Make sure before clear cache commits your code and push it to the server.

git rm -r --cached .
git add .
git commit -am 'git cache cleared'
git push origin master

If you face any kind of issue after following above step please write below the comment.

Git remove last commit from remote and local

Git how to remove the last commit from remote and local. Sometimes we are creating the mistake on written commit message or push wrong code by mistake at the time we need to remove that code or commit from remote and local. So let’s see the step for removing the last commit from remote or local.

Make sure before removing any comment from the remote server because that commit also removed for your team member when his push or pull the code from the remote server.

First of all get the git log

git log

The output will look like this on your command prompt

commit 6488b7e23c31f4472b063b8d3c3e15f8dfc91495
Author: thecodingstuff <thecodingstuff@gmail.com>
Date:   Thu Jul 20 10:17:39 2017 -0700

    Add welcome heading tag

commit 69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33
Author: thecodingstuff <thecodingstuff@gmail.com>
Date:   Thu Jul 20 10:14:36 2017 -0700

    first commit

Now copy the commit id like “69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33” from where you want to remove commit on remote or local server.

Now type below command for removing the commit.

git reset --hard 69f7c2b25ccf49dcd9ede9f21d33eb5c2b6e6f33
git push -f origin master

Replace “master” with your branch name.

After following this command your commit was removed from the remote and local server.

If you face any issue after using the above steps please write the comment below.