Join two tables and get count in mysql:Example with hasMany relations in sequelize

Consider we have two tables ‘users‘ and ‘comments‘.Each user can have multiple comments so that ‘users‘ and ‘comments‘  have ‘hasMany‘ relation. The foreign key is user_id in the comments table. The structure of both tables are as follows Users id    | name |    email 1    userone 2    usertwo Comments id    | comment        | user_id 1      commentone      1 2      commenttwo      1 3      commentthree    2 4      commentfour     2 5      commentfive      2 6      commentfive      2 As you can see, the user with id 1 has two comments and the user with id 2 has  4 comments. To get all users with their respective number of comments, we can use the following query.

The result will be 1 userone 2 2 usertwo 4 Sequelize Example   To learn more about sequelize, see the following article For our example, we need to create two model files usersModel.js


  hasMany relation Now we need to define hasMany relations from users to comments, in your db.js file….

Creating a Cron job in Laravel 5.2 :Task Scheduling
PHP , Beginner , 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…

Understanding Callbacks and Callback hell in JavaScript : asynchronous behaviour of Javascript Part -1

In most programming languages, functions can accept other function(s) as arguments or return a function. Such functions are called Higher-order functions. In JavaScript, most APIs and Modules can accept a function as an argument to a function. The functions are called callbacks. Structure of callbacks

Note that JavaScript functions behave as objects. We can set a function as an object property or store a function into a variable. JavaScript is a non-blocking programming language. It doesn’t wait or block operation to finish but keeps on executing. For example, consider the case when data is fetched asynchronously, then javascript doesn’t wait for the asynchronous call to get data but keeps executing the next line of code. So in order to do something with the data, we can use callbacks. See the below example in which we use a timeout to wait for the data just like an XHR request which may take some time to complete

Asynchronous callback with  Jquery and a callback function

  CALLBACK HELL Consider the above example in which we use a callback function which is passed as an argument to the $.get function in Jquery. Now what happens when we want to send subsequent requests is that…

Prototypes and Prototypal inheritance in Javascript

The word prototype means an early model or a primary version of a machine or a product.All further version created from that prototype will inherit the properties of that prototype.Prototype-based programming is a style of object-oriented programming in which behaviour reuse is performed via a process of reusing existing objects via delegation that serve as prototypes.(Wikipedia). Nearly all objects in JavaScript are instances of Object which sits on the top of a prototype chain.The Object.prototype is on the top of the prototype inheritance chain. That means almost all the objects in JavaScript inherit the properties and methods from Object.prototype.JavaScript Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Prototype determines the behaviour of a data object.Properties including methods are inherited from the prototype.See the below array which inherits prototypes from Array.prototype.You can see is array,map,reduce etc which are inherited from Array.prototype Prototypal inheritance In Javascript a function is a first-class Object.All JavaScript objects inherit properties and methods from a prototype.

In the example above, function Person() is called object constructor function.Wth the new keyword,we can create new objects fusing the constructor function.

both myGo and myGoGo has properties first,last,age,eyecolor and  prototype We cannot add a new property to an Person directly.we can add it to its prototype.

“use strict” – Understanding Strict Mode in JavaScript
interview questions , Javascript / June 10, 2018

While using certain JavaScript libraries,you might have seen some thing like

  inside the code.Many of us doesn’t exactly know what this exactly mean.In this article we take a look at the JavaScript  strict mode(ECMAScript 5) which might become handy while  appearing in JavaScript Developer Interviews. Strict mode literal expression, ignored by earlier versions of JavaScript,  lets us create secure JavaScript code.It  enforces better programming practices and removes some of the language’s insecure and ill-advised features.Strict mode throws exception to some of the basic coding bloopers and throws error to some insecure actions that are usually ignored in normal mode. Use strict can be used on top of the file or can be used inside a function in the following way 1  In a file

2 Specifically inside a function

Things that are prevented in strict  1  It prevents use of variables with out declaration.

2 Deleting function or variable is not allowed in strict mode  

3 Prevent global  access

4 Duplicating object properties not allowed

5 Cannot duplicate parameters of a function


6 eval is not allowed to create variable

7  Writing…

Spread syntax(…) in JavaScript(ES6) – Explained

Spread syntax helps to expand iterables (Arrays, Objects, Strings) in places where zero or more entities are expected. Consider an example with array.

Here 3 arguments are expected and we store them into an array

Now we use spread syntax to provide arguments in the array

The expected result is 6     Now while working with spread syntax on objects Merge two objects with spread syntax.

For cloning the object

    Now working with spread syntax on strings Strings are also iterable and using spread operators will make them spread into individual characters.

JavaScript objects and arrays are mutable. If you assign a JavaScript array into another variable and push a value into it, both the first one and second one will change. Actually, this is happening because both the variables have the same reference. See the example below

In the above example, a new number,5 is pushed into the array. Here you can see both the variables have changed because both have the same reference. The spread operator creates a new copy of the array with different reference. Using the spread(…) operator we can achieve what we need without this…

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


Javascript snippet for extracting image (url) from string – regex
Javascript / June 2, 2018

Consider the following string

The above code extracts all the images in the string  as an array and we take  one from it;  

Array reduce functions in Javascript
Javascript / June 1, 2018

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.Reduce function makes it to a single value.

Here we can see reduce always accepts a call back function. The variable a  is called the accumulator in which the return value is stored The variable b represents each variable inside the array.Thus what reduce does is that it runs a function on each values inside the array. The return value of the function is stored in an accumulator (result/total). Now we take a look at how to calculate the sum of squares of all the values present inside the array.So that you get a better understanding of what happens inside the reduce function.This time we define the function seperately.

  The above returns sum of squares of all values inside the array.

Array map functions in Javascript
Javascript / June 1, 2018

Conside a Javascrpt Array of numbers.We need to make an array which contains squares of all these numbers.What we can do to achieve this is using a for loop.

For doing these Javascript has another function called MAP function on array.Using the map function the above result can be achieved in the following way

Array Map on Array of objects in Javascript


Share this page in social media platforms