Monday, 27 March 2023

Laravel queue with job and supervisor

 https://learn2torials.com/a/how-to-setup-laravel-supervisor#:~:text=Supervisor%20is%20basically%20linux%20based,supervisor%20know%20about%20background%20jobs.&text=Make%20sure%20in%20above%20file,linux%20logged%20in%20user%20id.



https://github.com/laravel/ideas/issues/1570


https://medium.com/@sreedevr/keep-laravel-queue-system-running-on-server-9b225e6b4f8c


Thursday, 16 March 2023

Add style to sitemap xml file

 

<?xml-stylesheet type="text/xsl" href="gss.xsl"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">


gss.xsl file
<?xml version="1.0" encoding="UTF-8"?>

<!-- Google Sitemaps Stylesheets (GSStylesheets)
     Project Home: http://sourceforge.net/projects/gstoolbox
     Copyright (c) 2005 Baccou Bonneville SARL (http://www.baccoubonneville.com)
     License http://www.gnu.org/copyleft/lesser.html GNU/LGPL
     
     Created by Serge Baccou
     1.0 / 20 Aug 2005
       
     Changes by Johannes Müller ( http://GSiteCrawler.com/ )
     1.1 / 20 Aug 2005 - sorting by clicking on column headers
                       - open urls in new window/tab 
                       - some stylesheet/CSS cleanup 
     
     Changes by Tobias Kluge ( http://enarion.net/ )
     1.2 / 22 Aug 2005 - moved sitemap file and sitemap index file into one file gss.xsl
	 1.5 / 27 Aug 2005 - added js and css into xslt stylesheet; only gss.xsl is needed now
     
     Changes by Serge Baccou
     1.3 / 23 Aug 2005 - some XSLT cleanup
     1.4 / 24 Aug 2005 - sourceForge and LGPL links and logos
                       - sorting is working for siteindex (see gss.js)


     Johannes Müller ( http://gsitecrawler.com/ )
     1.5a/ 31 Aug 2005 - added version number in footer
                       - removed images (don't allow tracking on other servers)
     1.5b/ 05 Jul 2006 - removed (unnecessary) link to (missing) CSS file
                       - moved necessary items from Googles CSS file intern
                       - javascript code not compatible with Opera 9.0
     1.6/  19 Nov 2006 - Changed namespace to http://www.sitemaps.org/schemas/sitemap/0.9

-->

<xsl:stylesheet version="2.0" 
                xmlns:html="http://www.w3.org/TR/REC-html40"
                xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="html" version="1.0" encoding="iso-8859-1" indent="yes"/>
  
  <!-- Root template -->    
  <xsl:template match="/">
    <html>     
      <head>  
        <title>Google Sitemap File for </title>
		<style type="text/css">
		  <![CDATA[
			<!--
			body {
				font-family: arial, sans-serif;
				font-size: 0.8em;
				height:100%;
			}
			body * {
				font-size: 100%;
			}
			h1 { 
				font-weight:bold;
				font-size:1.5em;
				margin-bottom:0;
				margin-top:1px; }
			
			h2 { 
				font-weight:bold;
				font-size:1.2em;
				margin-bottom:0; 
				color:#707070;
				margin-top:1px; }

			h3 { 
				font-weight:bold;
				font-size:1.2em;
				margin-bottom:0; 
				color:#000;
				margin-top:1px; }
				
			td, th {
				font-family: arial, sans-serif;
				font-size: 0.9em;
			}			
			
			.header {
				font-weight: bold;
				font-size: 1.1em;
			}
			
			p.sml { 
				font-size:0.8em;
				margin-top:0; 
			}
			
			.data {
				border-collapse:collapse;
				border:1px solid #b0b0b0;
				margin-top:3px;
				width:100%;
				padding:5em;
			}

			.data td {
				border-bottom:1px solid #b0b0b0;
				text-align:left;
				padding:3px;
			}
			
			.sortup {
				background-position: right center;
				background-image: url(http://www.google.com/webmasters/sitemaps/images/sortup.gif);
				background-repeat: no-repeat;
				font-style:italic;
				white-space:pre; }
				
			.sortdown {
				background-position: right center;
				background-image: url(http://www.google.com/webmasters/sitemaps/images/sortdown.gif);
				background-repeat: no-repeat;
				font-style:italic;
				white-space:pre; }
			
			table.copyright {
				width:100%;
				border-top:1px solid #ddad08;
				margin-top:1em;
				text-align:center;
				padding-top:1em;
				vertical-align:top; }
				
			.copyright {
				color: #6F6F6F;
				font-size: 0.8em;
			}
			-->
		  ]]>
		</style>
        <script language="JavaScript">
		  <![CDATA[
			var selectedColor = "blue";
			var defaultColor = "black";
			var hdrRows = 1;
			var numeric = '..';
			var desc = '..';
			var html = '..';
			var freq = '..';
			
			function initXsl(tabName,fileType) {
				hdrRows = 1;
			
			  if(fileType=="sitemap") {
			  	numeric = ".3.";
			  	desc = ".1.";
			  	html = ".0.";
			  	freq = ".2.";
			  	initTable(tabName);
				  setSort(tabName, 3, 1);
			  }
			  else {
			  	desc = ".1.";
			  	html = ".0.";
			  	initTable(tabName);
				  setSort(tabName, 1, 1);
			  }
			
				var theURL = document.getElementById("head1");
				theURL.innerHTML += ' ' + location;
				document.title += ': ' + location;
			}
			
			function initTable(tabName) {
			  var theTab = document.getElementById(tabName);
			  for(r=0;r<hdrRows;r++)
			   for(c=0;c<theTab.rows[r].cells.length;c++)
			     if((r+theTab.rows[r].cells[c].rowSpan)>hdrRows)
			       hdrRows=r+theTab.rows[r].cells[c].rowSpan;
			  for(r=0;r<hdrRows; r++){
			    colNum = 0;
			    for(c=0;c<theTab.rows[r].cells.length;c++, colNum++){
			      if(theTab.rows[r].cells[c].colSpan<2){
			        theCell = theTab.rows[r].cells[c];
			        rTitle = theCell.innerHTML.replace(/<[^>]+>|&nbsp;/g,'');
			        if(rTitle>""){
			          theCell.title = "Change sort order for " + rTitle;
			          theCell.onmouseover = function(){setCursor(this, "selected")};
			          theCell.onmouseout = function(){setCursor(this, "default")};
			          var sortParams = 15; // bitmapped: numeric|desc|html|freq
			          if(numeric.indexOf("."+colNum+".")>-1) sortParams -= 1;
			          if(desc.indexOf("."+colNum+".")>-1) sortParams -= 2;
			          if(html.indexOf("."+colNum+".")>-1) sortParams -= 4;
			          if(freq.indexOf("."+colNum+".")>-1) sortParams -= 8;
			          theCell.onclick = new Function("sortTable(this,"+(colNum+r)+","+hdrRows+","+sortParams+")");
			        }
			      } else {
			        colNum = colNum+theTab.rows[r].cells[c].colSpan-1;
			      }
			    }
			  }
			}
			
			function setSort(tabName, colNum, sortDir) {
				var theTab = document.getElementById(tabName);
				theTab.rows[0].sCol = colNum;
				theTab.rows[0].sDir = sortDir;
				if (sortDir) 
					theTab.rows[0].cells[colNum].className='sortdown'
				else
					theTab.rows[0].cells[colNum].className='sortup';
			}
			
			function setCursor(theCell, mode){
			  rTitle = theCell.innerHTML.replace(/<[^>]+>|&nbsp;|\W/g,'');
			  if(mode=="selected"){
			    if(theCell.style.color!=selectedColor) 
			      defaultColor = theCell.style.color;
			    theCell.style.color = selectedColor;
			    theCell.style.cursor = "hand";
			    window.status = "Click to sort by '"+rTitle+"'";
			  } else {
			    theCell.style.color = defaultColor;
			    theCell.style.cursor = "";
			    window.status = "";
			  }
			}
			
			function sortTable(theCell, colNum, hdrRows, sortParams){
			  var typnum = !(sortParams & 1);
			  sDir = !(sortParams & 2);
			  var typhtml = !(sortParams & 4);
			  var typfreq = !(sortParams & 8);
			  var tBody = theCell.parentNode;
			  while((tBody.nodeName!="TBODY") && (tBody.nodeName!="TABLE")) {
			    tBody = tBody.parentNode;
			  }
			  var tabOrd = new Array();
			  if(tBody.rows[0].sCol==colNum) sDir = !tBody.rows[0].sDir;
			  if (tBody.rows[0].sCol>=0)
			    tBody.rows[0].cells[tBody.rows[0].sCol].className='';
			  tBody.rows[0].sCol = colNum;
			  tBody.rows[0].sDir = sDir;
			  if (sDir) 
			  	 tBody.rows[0].cells[colNum].className='sortdown'
			  else 
			     tBody.rows[0].cells[colNum].className='sortup';
			  for(i=0,r=hdrRows;r<tBody.rows.length;i++,r++){
			    colCont = tBody.rows[r].cells[colNum].innerHTML;
			    if(typhtml) colCont = colCont.replace(/<[^>]+>/g,'');
			    if(typnum) {
			      colCont*=1;
			      if(isNaN(colCont)) colCont = 0;
			    }
			    if(typfreq) {
					switch(colCont.toLowerCase()) {
						case "always":  { colCont=0; break; }
						case "hourly":  { colCont=1; break; }
						case "daily":   { colCont=2; break; }
						case "weekly":  { colCont=3; break; }
						case "monthly": { colCont=4; break; }
						case "yearly":  { colCont=5; break; }
						case "never":   { colCont=6; break; }
					}
				}
			    tabOrd[i] = [r, tBody.rows[r], colCont];
			  }
			  tabOrd.sort(compRows);
			  for(i=0,r=hdrRows;r<tBody.rows.length;i++,r++){
			    tBody.insertBefore(tabOrd[i][1],tBody.rows[r]);
			  } 
			  window.status = ""; 
			}
			
			function compRows(a, b){
			  if(sDir){
			    if(a[2]>b[2]) return -1;
			    if(a[2]<b[2]) return 1;
			  } else {
			    if(a[2]>b[2]) return 1;
			    if(a[2]<b[2]) return -1;
			  }
			  return 0;
			}

		  ]]>
		</script>
        
      </head>

      <!-- Store in $fileType if we are in a sitemap or in a siteindex -->
      <xsl:variable name="fileType">
        <xsl:choose>
		  <xsl:when test="//sitemap:url">sitemap</xsl:when>
		  <xsl:otherwise>siteindex</xsl:otherwise>
        </xsl:choose>      
      </xsl:variable>            

      <!-- Body -->
      <body onLoad="initXsl('table0','{$fileType}');">  
            
        <!-- Text and table -->
        <h1 id="head1">Google Sitemap</h1>        
        <xsl:choose>
	      <xsl:when test="$fileType='sitemap'"><xsl:call-template name="sitemapTable"/></xsl:when>
	      <xsl:otherwise><xsl:call-template name="siteindexTable"/></xsl:otherwise>
  		</xsl:choose>
          
        <!-- Copyright notice -->          
        <br/>
        
      </body>
    </html>
  </xsl:template>     

  <!-- siteindexTable template -->
  <xsl:template name="siteindexTable">
    <h3>This sitemap index file was created by <a href="http://gsitecrawler.com/">SOFTplus GSiteCrawler</a>.</h3>          
    <h2>Number of sitemaps in this Google sitemap index: <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"></xsl:value-of></h2>          
    <p class="sml">Click on the table headers to change sorting.</p>
    <table border="1" width="100%" class="data" id="table1">
      <tr class="header">
        <td>Sitemap URL</td>
        <td>Last modification date</td>
      </tr>
      <xsl:apply-templates select="sitemap:sitemapindex/sitemap:sitemap">
        <xsl:sort select="sitemap:lastmod" order="descending"/>              
      </xsl:apply-templates>  
    </table>            
  </xsl:template>  
  
  <!-- sitemapTable template -->  
  <xsl:template name="sitemapTable">
            
    <h2>Number of URLs in this Google Sitemap: <xsl:value-of select="count(sitemap:urlset/sitemap:url)"></xsl:value-of></h2>
    <p class="sml">Click on the table headers to change sorting.</p>
    <table border="1" width="100%" class="data" id="table0">
	  <tr class="header">
	   	    <td>Sitemap URL</td>
		<td>Change freq.</td>
	
	  </tr>
	  <xsl:apply-templates select="sitemap:urlset/sitemap:url">
	    <xsl:sort select="sitemap:priority" order="descending"/>              
	  </xsl:apply-templates>
	</table>  
  </xsl:template>    
  
  <!-- sitemap:url template -->  
  <xsl:template match="sitemap:url">
    <tr>  
      <td>
        <xsl:variable name="sitemapURL"><xsl:value-of select="sitemap:loc"/></xsl:variable>  
        <a href="{$sitemapURL}" target="_blank" ref="nofollow"><xsl:value-of select="$sitemapURL"></xsl:value-of></a>
      </td>
      <td><xsl:value-of select="sitemap:changefreq"/></td>
   </tr>  
  </xsl:template>
  
  <!-- sitemap:sitemap template -->
  <xsl:template match="sitemap:sitemap">
    <tr>  
      <td>        
        <xsl:variable name="sitemapURL"><xsl:value-of select="sitemap:loc"/></xsl:variable>  
        <a href="{$sitemapURL}"><xsl:value-of select="$sitemapURL"></xsl:value-of></a>
      </td>
      <td><xsl:value-of select="sitemap:lastmod"/></td>
    </tr>  
  </xsl:template>  
  
</xsl:stylesheet>

Wednesday, 15 March 2023

laravel Date Compare with created at field timestamp


1)-------------------------------------------------- public function index(Request $request) { $startDate = '2022-01-01'; $endDate = '2022-07-30'; $posts = Post::whereBetween('created_at', [$startDate, $endDate])->get(); return $posts; }


2)--------------------------------------------------

public function index(Request $request)

    {

        $startDate = Carbon::createFromFormat('Y-m-d', '2022-06-01')->startOfDay();

        $endDate = Carbon::createFromFormat('Y-m-d', '2022-06-30')->endOfDay();


        $posts = Post::whereBetween('created_at', [$startDate, $endDate])->get();

        return $posts;

    }

3)-------------------------------------------------

public function index(Request $request)

    {

        $startDate = Carbon::createFromFormat('Y-m-d', '2022-06-01');

        $endDate = Carbon::createFromFormat('Y-m-d', '2022-06-30');

        

        $posts = Post::whereDate('created_at', '>=', $startDate)

            ->whereDate('created_at', '<=', $endDate)

            ->get();

        return $posts;

    }

4)--------------------------------------------

public function index(Request $request)

    {

        $startDate = Carbon::createFromFormat('Y-m-d', '2022-06-01');

        $endDate = Carbon::createFromFormat('Y-m-d', '2022-06-30');

        $users = User::whereDate('start_at', '>=', $startDate)

                ->whereDate('end_at', '<=', $endDate)

                ->get();


        return $users;

    }

5)-------------------------------------------

SELECT * FROM posts WHERE DATE(created_at) BETWEEN '2022-06-01' AND '2022-06-30'


6)------------------------------------------------------------------------
$startDate = '2022-06-01';
$endDate = '2022-06-30';

Post::whereBetween(DB::raw('DATE(created_at)'), [$startDate, $endDate])->get();

Thursday, 9 March 2023

laravel RouteServiceprovider redirect() not working

RouteServiceProvider.php 



public function boot()
    {
       $str= request()->getRequestUri();
       if(str_contains($str, '/index.php'))
       {
           header("Location: https://www.example.com/");
           exit();
       }
       if(str_contains($str, '/public/'))
       {
           header("Location: https://www.example.com/");
           exit();
       }
   
        parent::boot();
    }

Force Non www to www in htaccess

 # Redirect non-www to www:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

The contrary is also possible (www to non-www):

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Monday, 6 March 2023

Git bitbucket create new branch below master branch

1) Clone the repo in new direcory

 git clone {repo_url}

2)go inside the cloned directory 

cd growth_plus_php/

3) create new branch

git checkout -b growthplus_live

4) delete all directories inside the cloned project except .git folder and .gitignore file

5)create new project or copy paste direcories of existing project

6)Check if  newly created branch is exist

 git branch

You will see the master branch  as well as newly created branch

7) add all the changes

git add .

8) commit all changes 

git commit -m "update"8

9) git push origin new_branch

Thursday, 2 March 2023

Laravel compare database column string date with date with query builder DB

Laravel query

 

 

$billdate_raw = DB::raw("STR_TO_DATE(`bill_date`, '%d/%m/%Y')");
        $start_raw = DB::raw("STR_TO_DATE(?, '%d/%m/%Y')");
        $end_raw = DB::raw("STR_TO_DATE(?, '%d/%m/%Y')");
        $fromDate=$request->fromDate;
        $toDate=$request->toDate;
        $query = DB::table($id)
            ->select(
                "{$id}.*",
                'users.name as UserName',
                'users.emp_code as UserEmpCode',
                'users.email as UserEmail',
                'users.mobile as UserMobile',
                'users.mobile as UserMobile',
                'departments.department_name as UserDepartment',
                'claim_statuses.approval_one_status as ApprovedStatusOne',
                'claim_statuses.approval_one_code as ApproverCodeOne',
                'claim_statuses.approval_one_time as ApprovalTimeOne',

                'claim_statuses.approval_two_status as ApprovedStatusTwo',
                'claim_statuses.approval_two_code as ApproverCodeTwo',
                'claim_statuses.approval_two_time as ApprovalTimeTwo',

                'claim_statuses.approval_three_status as ApprovedStatusThree',
                'claim_statuses.approval_three_code as ApproverCodeThree',
                'claim_statuses.approval_three_time as ApprovalTimeThree'
            );
            if($request->fromDate !=null)
            {
                $query->whereBetween($billdate_raw, [$start_raw, $end_raw])
                ->setBindings(["$fromDate", "$toDate"]);
            }
         
            $query->join('users', 'users.user_id', '=', "{$id}.user_id")
            ->join('departments', 'departments.department_id', '=', 'users.department_id')
            ->join('claim_statuses', 'claim_statuses.table_id', '=', "{$id}.id")
            ->where('claim_statuses.table_name',$id);
           
           $data= $query->get();

 Plain sql query

$sqlQuery=DB::select('
           SELECT
                `internet_expense`.*,
                `users`.`name` AS `UserName`,
                `users`.`emp_code` AS `UserEmpCode`,
                `users`.`email` AS `UserEmail`,
                `users`.`mobile` AS `UserMobile`,
                `users`.`mobile` AS `UserMobile`,
                `departments`.`department_name` AS `UserDepartment`,
                `claim_statuses`.`approval_one_status` AS `ApprovedStatusOne`,
                `claim_statuses`.`approval_one_code` AS `ApproverCodeOne`,
                `claim_statuses`.`approval_one_time` AS `ApprovalTimeOne`,
                `claim_statuses`.`approval_two_status` AS `ApprovedStatusTwo`,
                `claim_statuses`.`approval_two_code` AS `ApproverCodeTwo`,
                `claim_statuses`.`approval_two_time` AS `ApprovalTimeTwo`,
                `claim_statuses`.`approval_three_status` AS `ApprovedStatusThree`,
                `claim_statuses`.`approval_three_code` AS `ApproverCodeThree`,
                `claim_statuses`.`approval_three_time` AS `ApprovalTimeThree`
            FROM
                `internet_expense`
            INNER JOIN `users` ON `users`.`user_id` = `internet_expense`.`user_id`
            INNER JOIN `departments` ON `departments`.`department_id` = `users`.`department_id`
            INNER JOIN `claim_statuses` ON `claim_statuses`.`table_id` = `internet_expense`.`id`
            WHERE
                STR_TO_DATE(`bill_date`, "%d/%m/%Y") BETWEEN STR_TO_DATE("01/01/2023", "%d/%m/%Y") AND STR_TO_DATE("03/01/2023", "%d/%m/%Y")
                AND `claim_statuses`.`table_name` = "internet_expense";');
           
            dd($sqlQuery);

Laravel Export data to csv

 use Illuminate\Http\Response; // Define a function to export data to CSV function exportToCSV($exportData, $columns) {     $filename = ...