Creating a Cron job in Laravel 5.2 :Task Scheduling
Beginner , PHP , Snippets / July 23, 2018

To create a cron job in Laravel 5.2 ,move to the app/console/ folder.The console folder contains all the relevant configuration files and folders for you to create a custom artisan command in Laravel 5.6 .  After creating the custom artisan command ,we can schedule the command to be run in specific intervals Step 1 Run

  to create new custom artisan command,This will generate a class called app/Console/Commands/CronJob.php.The newly generated file will have the following structure.You have to fill in name and description variables in the file.You can use the DB and MODEL files from CronJob.php. The file extends the Command Class   app/Console/Commands/CronJob.php

Here we select a set of users based on some custom criteria from the users table and do something to them periodically with the cron job.The action for the should be written in the CronJob.php – handle() function.The class defines our command.To run this need to use the following php artisan cronjob Step 2 Scheduling the job To schedule the when to run the command we have created open the kernel.php file in the app/console/ folder app/console/kernel.php

The kernel class extends ConsoleKernel .To add the commands

and to schedule the command

  This runs…

How to display user – display name,avatar and logout button in WordPress : Example
Beginner , PHP , Snippets , Wordpress / July 8, 2018

To display user displayname , avatar and provide a logout button for a user in your wordpress site,we can use the following  five worpress functions. is_user_logged_in () –  Check whether used in. wp_get_current_user(id/email,null)  –  Get current logged in user get_avatar_url(userid) – Get avatar url for current user using userid wp_logout_url(url) – Logout and redirect URL home_url() – Get your home URL  

  Check below links to see documentation in full https://developer.wordpress.org/reference/functions/is_user_logged_in/ https://developer.wordpress.org/reference/functions/wp_get_current_user/ https://developer.wordpress.org/reference/functions/get_user_by/ https://developer.wordpress.org/reference/functions/wp_logout_url/        

How to change ‘sql_mode’ in mysql
PHP , Uncategorized / July 2, 2018

To see Mysql mode settings,do the following query. Mysql mode is not linked to any particular database. Modes can be set locally to the session (connection), or for the server globally.

This gives results like below   Now to change sql_mode,you need to run query like this

Example Some times after  importing database and changing some table structure,you may get an error like this. This is because NO_ZERO_IN_DATE and NO_ZERO_DATE is enabled in and database has invalid date values. NO_ZERO_IN_DATE The NO_ZERO_IN_DATE mode affects whether the server permits dates in which the year part is nonzero but the month or day part is 0. Example – ‘2010-00-01’ or ‘2010-01-00’ NO_ZERO_DATE This mode is deprecated This determines whether the server permits ‘0000-00-00’ . NO_ZERO_IN_DATE also depends on whether strict SQL mode is enabled. Removing above modes from sql_mode  lets the server allow invalid dates in the aforementioned format     Or to set it PERMANANTLY, change etc/my.cnf

and restart WHAT is sql_mode Server SQL modes determine what SQL syntax MySQL should support and what kind of data validation checks it should use. This makes it easier to use MySQL in different environments. See more on  list of sql…

What are cookies and how to set and get them with Javascript

Cookies are data stored as text files in the computer.They are used store data that are needed to be remembered , like user information.Cookies can be set in client side and server side. For example,consider a user sign in to your website.The server sends back some response.The response have a token/id that can be stored as a cookie.This cookie value can keep the users details status in the client side. Cookie has a name and value pair,expiry time,path and secure . The security flags that are used to provide more security HttpOnly – HttpOnly flag sets the cookie unavailable to client side scripts.This prevent cross site scripting attacks(XSS) secure – Sets cookie to be send via a secure encrypted channel.Browser will send cookie only via an HTTPS request and prevent sending cookie via HTTP request. setcookievalue This function has three arguments name – cookie name. value – value of the cookie. hours- hours determine the expiry hours. document.cookie lets us store the cookie associated with the current document. getCookie This function has one argument ,cookie name document.cookie gets all the cookies associated with the current document.A cookie with a specific name can be obtained using the getCookie function.   getCookievalue This function has…

An introduction to Eloquent ORM in Laravel
interview questions , PHP , Uncategorized / June 20, 2018

ORM refers to Object Relational Mapping.What ORM tools like  Eloquent does is that they are equipped with a metadata descriptor which helps in connecting object code to a relational database.To learn more on ORM refer this article. https://truetocode.com/orm-using-sequelize-orm-with-express-node-js/418/ Now,we know that sequelize is an ORM tool that is used to connect node applications to relational databases such as Mysql/postgres.For PHP Developers ,Laravel is equipped with a very simple,efficient ORM called Eloquent ORM. Using Eloquent To create model class we use the following command

Now in case you dont know,Artisan is a command line interface provided with Laravel just like the other command line interfaces you might have used,like angular-cli,Artisan helps you to generate files while working with Laravel. The above command creates a model file for our Table and it looks like this

As you can see all models extend “Illuminate\Database\Eloquent\Model” Also note that the table has ‘snake case plural’ name of the class.That is ‘televisions’ table and ‘television’ class.Uncomment the $table variable in the above code to use custom table name. Now,To generate a migration script on creating the model.This can be done using the following command.

The migration file looks like this

To run the…

Implementing priority queue in PHP
PHP , programming concepts / June 2, 2018

Queue is a First In First Out(FIFO) Data Structure.In a priority queue, an element with high priority is served before an element with low priority.The Standard PHP Library has the SplPriorityQueue class which can be used to implement priority queue.The insert(‘item’,priority) method is used to insert item and the corresponding priority in to the queue.See the below example to see mail sending to different mail id’s that have different priorities.You can use while to iterate thorough the queue and process each one or the results. SplPriorityQueue A priority queue can be implemented using many of the data structures that we’ve already studied (array, inked list, binary search tree). However, those data structures do not provide the most efficient operations. To make all of the operations  efficient, heap can be used . SplPriorityQueue is implemented using Max Heap

     

OOPS and PHP
PHP / May 31, 2018

In this section we will look at OOP with PHP . Refering the below explanations prior to interviews will surely help you to answer some of the most common questions an rookie/intermediate level developer might face in interviews.Here is a diagram depicting object oriented programming concepts CLASSES In object-oriented programming , a class is a template definition of the method s and variables in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables.

A CLASS is a template of an object.An OBJECT  is an instance of class

DATA ABSTRACTION Concept of Abstraction in PHP is important php oop (object oriented programming) concept.To show the needed/relevant information or details without showing all information which is not necessary is called abstraction.   Abstract Classes. – An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

    The constructor of a class defines what happens when you instantiate an object from this class. The destructor of a class defines what…

Share this page in social media platforms